note 57013 added to ref.mysql

From: Date: Wed, 21 Sep 2005 17:47:27 +0000
Subject: note 57013 added to ref.mysql
Groups: php.notes 
Request: Send a blank email to php-notes+get-95548@lists.php.net to get a copy of this message
This is a small function I wrote to handle queries on a table. It can query a table, order and sort, and supports inner joins. This function also returns the result as a single row or all rows. Enjoy : <?php /** * Selects Fields from a database/table. * Also supports INNER JOINS, GROUP BY, ORDER BY. * * @Author : Jonathon Hibbard * * @param string $db_name //Name of Database * @param string $table //Name of Table * @param array $fields //Field Names we want to select * @param array $inner //Fields to be INNER JOINED * @param array $where //WHERE fields. * @param string $group_by //Field to group by. * @param string $order_by //Field to order by * @param string $asc //Asc or Desc order * @param string $return_array //Defines $rst as an array or single row * @return $rst //Returns the result * * @example : (1) getFields('mytool','master', * 'array('master.master_id','master.fname_id')); * (2) getFields('mytool','master', * 'array('master.master_id','master.fname_id', * 'fname.fname'), array(0=>array('mytool', * 'fname','master','fname_id',)), * 'master.default_value <> "", false); * (3) getFields('mytool','master','master.master_id',NULL, * 'master.fname_id = "3",NULL,NULL,NULL,false); */ function getFields($db_name=NULL,$table=NULL,$fields=NULL,$inner=NULL, $where=NULL,$group_by=NULL,$order_by=NULL,$asc=NULL, $return_array=true) { $C_NAME = __CLASS__."::".__FUNCTION__; $sql = "SELECT "; is_array($fields) ? $sql .= implode(',',$fields) : $sql .= $fields; $sql .= " FROM ".$db_name.".".$table; if(!empty($inner)) { $total_inners = count($inner); for($i=0; $i<$total_inners; $i++) $sql .= " INNER JOIN ".$inner[$i][0].".".$inner[$i][1]." ON ".$inner[$i][1].".".$inner[$i][2]." = ".$inner[$i][3].".".$inner[$i][2]; } if(!empty($where)) { is_array($where) ? $sql .= " WHERE ".implode(' AND ',$where) : die("Fatal Error : Variable must be an array. Variable type is : ".gettype($where)); } !empty($group_by) ? $sql .= " GROUP BY ".$group_by : ''; !empty($order_by) ? $sql .= " ORDER BY ".$order_by : ''; !empty($asc) ? $sql .= " ".$asc : ''; $sql .= " # Query resides in ".__FILE__."->".$C_NAME." on line ".__LINE__; if($return_array === true) { $rst = mysql_query($sql) or die("An Error Has Occurred! \n MySQL Error Reports : ".mysql_error()." \n The Error Occured in ".__FILE__." -> ".$C_NAME); $i=0; while($row = mysql_fetch_assoc($rst)) { $rowRst[] = $row[$fields[$i]]; $i++; } } else { $rst = mysql_query($sql) or die("An Error Has Occurred! \n MySQL Error Reports : ".mysql_error()." \n The Error Occured in ".__FILE__." -> ".$C_NAME); $rowRst = mysql_fetch_assoc($rst); } return $rowRst; } ?> ---- Manual Page -- http://www.php.net/manual/en/ref.mysql.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+57013 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+57013&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+57013&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+57013&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+57013&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+57013&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+57013&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+57013&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+57013&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#95548) next »