Recommend this page to a friend! |
Ultimate MySQL | > | All threads | > | 2 added functions [SoluNeXT] | > | (Un) Subscribe thread alerts |
|
DENIS CARON - 2014-09-06 08:09:22
Hi,
Thanx for your work, it's great. I've just passed to the 3.0 version, and added my two own functions... As it could be usefull for community, here they are. /** * Get first rows in a table based on a WHERE filter and return an object PHP resource object * * @param string $tableName The name of the table * @param array $whereArray (Optional) An associative array containing the * column names as keys and values as data. The * values must be SQL ready (i.e. quotes around * strings, formatted dates, ect) * @param array/string $columns (Optional) The column or list of columns to select * @param array/string $sortColumns (Optional) Column or list of columns to sort by * @param boolean $sortAscending (Optional) TRUE for ascending; FALSE for descending * This only works if $sortColumns are specified * @param integer/string $limit (Optional) The limit of rows to return * @return object PHP resource object containing the first row or * FALSE if no row is returned from the query */ public function SelectSingleRow($tableName, $whereArray = null, $columns = null, $sortColumns = null, $sortAscending = true, $limit = null) { $ok = $this->SelectRows($tableName, $whereArray, $columns, $sortColumns, $sortAscending, 1); if (!$ok) return false; return $this->Row(); } /** * Gets rows in a table based on a WHERE filter and return an array with object PHP resource object for each records * * @param string $tableName The name of the table * @param array $whereArray (Optional) An associative array containing the * column names as keys and values as data. The * values must be SQL ready (i.e. quotes around * strings, formatted dates, ect) * @param array/string $columns (Optional) The column or list of columns to select * @param array/string $sortColumns (Optional) Column or list of columns to sort by * @param boolean $sortAscending (Optional) TRUE for ascending; FALSE for descending * This only works if $sortColumns are specified * @param integer/string $limit (Optional) The limit of rows to return * @return array of object PHP resource object on success or FALSE on error */ public function SelectAllRows($tableName, $whereArray = null, $columns = null, $sortColumns = null, $sortAscending = true, $limit = null) { $ok = $this->SelectRows($tableName, $whereArray, $columns, $sortColumns, $sortAscending, 1); if (!$ok) return false; $res=array(); while (! $this->EndOfSeek()) { $res[] = $this->Row(); } //if(count($res==1)) return $res[0]; // This line for first used for returning only object without array if query returns only one record, but it should crash caller, so... I removed it. return $res; } <?php Just add'em at the end of the class, and you'll earn coding time and remove code repetition. Exemple : for the first function... >>>Old version $filter=array(); $filter['name']=MySQL::SQLValue($username); $filter['pass']=MySQL::SQLValue($password); $req=MySQL::BuildSQLSelect('player',$filter); $res = $db->QuerySingleRow($req); >>>New version $filter=array(); $filter['name']=MySQL::SQLValue($username); $filter['pass']=MySQL::SQLValue($password); $res = $db->SelectSingleRow('player',$filter); Exemple : for the second one... >>>Old version $filter=array(); $filter['id']=MySQL::SQLValue($userId); $filter['units']=MySQL::SQLValue($unitType); $ok = $bd->SelectRows('unitspos',$filter); if($ok){ $db->MoveFirst(); $res=array(); while (! $db->EndOfSeek()) { $res[] = $db->Row(); } //Now $db is release for other queries and I have all my records for working... } else { //Oups I've got an error! } >>>New version $filter=array(); $filter['id']=MySQL::SQLValue($userId); $filter['units']=MySQL::SQLValue($unitType); $res = $bd->SelectAllRows('unitspos',$filter); if($res){ //Ready for working with all record, and $db is free for other queries... } else { //Oups I've got an error! } Usefull for me... Hope it should be for you! Regards Denis CARON SoluNeXT http://solunext.fr |
info at phpclasses dot org
.