note 74048 added to function.mysql-fetch-assoc

From: Date: Thu, 22 Mar 2007 00:50:25 +0000
Subject: note 74048 added to function.mysql-fetch-assoc
Groups: php.notes 
Request: Send a blank email to php-notes+get-124693@lists.php.net to get a copy of this message
function array2table: small fix to the post below that handles data returned from mysql that is either null or 0... This is a useful script for displaying MySQL results in an HTML table. <? function array2table($arr,$width) { $count = count($arr); if($count > 0){ reset($arr); $num = count(current($arr)); echo "<table align=\"center\" border=\"1\"cellpadding=\"5\" cellspacing=\"0\" width=\"$width\">\n"; echo "<tr>\n"; foreach(current($arr) as $key => $value){ echo "<th>"; echo $key."&nbsp;"; echo "</th>\n"; } echo "</tr>\n"; while ($curr_row = current($arr)) { echo "<tr>\n"; $col = 1; while (false !== ($curr_field = current($curr_row))) { echo "<td>"; echo $curr_field."&nbsp;"; echo "</td>\n"; next($curr_row); $col++; } while($col <= $num){ echo "<td>&nbsp;</td>\n"; $col++; } echo "</tr>\n"; next($arr); } echo "</table>\n"; } } ?> <? // Add DB connection script here $query = "SELECT * FROM mytable"; $result = mysql_query($query); while($row = mysql_fetch_assoc($result)){ $array[] = $row; } array2table($array,600); // Will output a table of 600px width ?> ---- Server IP: 64.71.164.2 Probable Submitter: 66.27.49.42 ---- Manual Page -- http://www.php.net/manual/en/function.mysql-fetch-assoc.php Edit -- https://master.php.net/note/edit/74048 Del: integrated -- https://master.php.net/note/delete/74048/integrated Del: useless -- https://master.php.net/note/delete/74048/useless Del: bad code -- https://master.php.net/note/delete/74048/bad+code Del: spam -- https://master.php.net/note/delete/74048/spam Del: non-english -- https://master.php.net/note/delete/74048/non-english Del: in docs -- https://master.php.net/note/delete/74048/in+docs Del: other reasons-- https://master.php.net/note/delete/74048 Reject -- https://master.php.net/note/reject/74048 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#124693) next »