note 35461 deleted from function.mysql-result by aidan
| From: | aidan@php.net | Date: | Fri, 09 Sep 2005 08:40:58 +0000 |
| Subject: | note 35461 deleted from function.mysql-result by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-94944@lists.php.net to get a copy of this message | ||
Note Submitter:
----
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...