note 35403 added to function.mysql-result

From: Date: Fri, 29 Aug 2003 22:07:03 +0000
Subject: note 35403 added to function.mysql-result
Groups: php.notes 
Request: Send a blank email to php-notes+get-55323@lists.php.net to get a copy of this message
Here is another way to show the results of a SELECT query in an HTML table implemented in a function, this formats the HTML a bit nicer than OMTELCOM's and also allows input on the same page: <html> <head> <title>Enter Query</title> </head> <body> <?php $host = 'localhost'; $name = 'name'; $pass = 'pass'; function drawtable($qr) { // $qr = results of a mysql_query $rows = mysql_num_rows($result); $toreturn = "<table style=\"float: left;\" border=\"2\">\n"; $toreturn .= "<tr>\n"; for ($i=0; $i<mysql_num_fields($qr); $i++) { $toreturn .= "\t<th>".mysql_field_name($qr,$i)."</th>\n"; } $toreturn .= "</tr>\n"; for ($i=0; $i<$rows; $i++) { $row = mysql_fetch_row($qr); $cols = sizeof($row); $toreturn .= "<tr>\n"; for ($x=0; $x < $cols; $x++) { if ($row[$x]==NULL){ $row[$x]='&nbsp;'; } $toreturn .= "\t<td>$row[$x]</td>\n"; } $toreturn .= "</tr>\n"; } $toreturn .= '</table>'; return $toreturn; } $q = stripslashes($_POST['q']); if (empty($q)) { $q = 'SELECT * FROM star_wars;'; } $db = mysql_connect($db,$name,$pass); if (!$db) { echo 'Could not connect to '.$host; exit; } mysql_select_db("test"); $result = mysql_query($q); echo drawtable($result); ?><table> <tr> <td><form method="POST" action="temp.php"> <textarea name="q" rows="25" cols="80">'.$q.'</textarea><br /> <input type="submit" value="Submit" /><input type="reset" value="Reset" /> </form></td> </tr> </table> </body> </html>'; ---- Manual Page -- http://www.php.net/manual/en/function.mysql-result.php Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+35403 Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+35403&report=yes Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+35403&report=yes

« previous php.notes (#55323) next »