note 96075 added to function.mysql-fetch-array

From: Date: Sun, 07 Feb 2010 12:27:48 +0000
Subject: note 96075 added to function.mysql-fetch-array
Groups: php.notes 
Request: Send a blank email to php-notes+get-165722@lists.php.net to get a copy of this message
Hey this is a simple function where you pass your table name and any number of coloumns you want and it will fetch the coloumns and return as a table. //function function fetch_records($table_name, $fields){ $conn = mysql_connect("localhost", "root", ""); $select_db = mysql_select_db("db", $conn); if(!$conn){ return "Not able to connect to dabase...."; } $table = "<table border='0' cellpadding='5' cellspacing='5'>"; $cols = implode (', ', $fields); $sql = "select $cols from $table_name"; $run = mysql_query($sql); if($run){ if(mysql_num_rows($run) == 0){ return "Sorry. No records found in the database"; } else { while($arr = mysql_fetch_array($run, MYSQL_ASSOC)){ $table .= "\t\t<tr>\n"; foreach ($arr as $val_col) { $table .= "\t\t\t".'<td>'.$val_col.'</td>'."\n"; } $table .= "\t\t</tr>\n"; } $table .= "</table>"; return $table; } mysql_free_result($run); } return "There was an error while fetching the records. Please contact site administrator."; } //how to call the function $table = "product_master"; $cols = array("product_name", "product_image", "product_price"); echo fetch_records($table, $cols); ---- Server IP: 69.147.83.197 Probable Submitter: 93.96.239.5 ---- Manual Page -- http://www.php.net/manual/en/function.mysql-fetch-array.php Edit -- https://master.php.net/note/edit/96075 Del: integrated -- https://master.php.net/note/delete/96075/integrated Del: useless -- https://master.php.net/note/delete/96075/useless Del: bad code -- https://master.php.net/note/delete/96075/bad+code Del: spam -- https://master.php.net/note/delete/96075/spam Del: non-english -- https://master.php.net/note/delete/96075/non-english Del: in docs -- https://master.php.net/note/delete/96075/in+docs Del: other reasons-- https://master.php.net/note/delete/96075 Reject -- https://master.php.net/note/reject/96075 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#165722) next »