note 35461 added to function.mysql-result
| From: | php-general at lists dot php dot net | Date: | Mon, 01 Sep 2003 21:16:03 +0000 |
| Subject: | note 35461 added to function.mysql-result | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-55443@lists.php.net to get a copy of this message | ||
For another way to display the results of a SELECT query in an HTML table, try:
------------------------------
<html>
<head>
<title>Enter Query</title>
</head>
<body>
<?php
$host = 'localhost';
$name = 'anonymous';
$pass = 'blargh';
function drawtable($qr) {
// $qr = results of a mysql_query
$rows = mysql_num_rows($qr);
$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]=' ';
}
$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);
echo '<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>';
-------------------
This particular example also has an input form on it, just change the user,password, and default
table (in the section where it checks if $q is empty) and run it. (It doesn't do any error
checking, and only works correctly with SELECT statements...
----
Manual Page -- http://www.php.net/manual/en/function.mysql-result.php
Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+35461
Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+35461&report=yes
Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+35461&report=yes