note 81987 added to function.mysql-result

From: Date: Sat, 22 Mar 2008 18:26:32 +0000
Subject: note 81987 added to function.mysql-result
Groups: php.notes 
Request: Send a blank email to php-notes+get-137327@lists.php.net to get a copy of this message
Here's how to display a PHP Mysql query result as HTML table <html><head><title>MySQL Table Viewer</title></head><body> <?php $db_host = 'localhost'; $db_user = 'root'; $db_pwd = 'lptm42b'; $database = 'sphinx'; $table = 'spheres'; if (!mysql_connect($db_host, $db_user, $db_pwd)) die("Can't connect to database"); if (!mysql_select_db($database)) die("Can't select database"); // sending query $result = mysql_query("SELECT * FROM {$table}"); if (!$result) { die("Query to show fields from table failed"); } $fields_num = mysql_num_fields($result); echo "<h1>Table: {$table}</h1>"; echo "<table border='1'><tr>"; // printing table headers for($i=0; $i<$fields_num; $i++) { $field = mysql_fetch_field($result); echo "<td>{$field->name}</td>"; } echo "</tr>\n"; // printing table rows while($row = mysql_fetch_row($result)) { echo "<tr>"; // $row is array... foreach( .. ) puts every element // of $row to $cell variable foreach($row as $cell) echo "<td>$cell</td>"; echo "</tr>\n"; } mysql_free_result($result); ?> </body></html> ---- Server IP: 200.185.126.110 Probable Submitter: 189.20.221.13 ---- Manual Page -- http://www.php.net/manual/en/function.mysql-result.php Edit -- https://master.php.net/note/edit/81987 Del: integrated -- https://master.php.net/note/delete/81987/integrated Del: useless -- https://master.php.net/note/delete/81987/useless Del: bad code -- https://master.php.net/note/delete/81987/bad+code Del: spam -- https://master.php.net/note/delete/81987/spam Del: non-english -- https://master.php.net/note/delete/81987/non-english Del: in docs -- https://master.php.net/note/delete/81987/in+docs Del: other reasons-- https://master.php.net/note/delete/81987 Reject -- https://master.php.net/note/reject/81987 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#137327) next »