SQL table listing
If you have prepared table by phpWebAdmin (from the last week), you can use script similar as:
<!DOCTYPE
HTML PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=windows-1250"> <meta name="generator" content="PSPad editor, www.pspad.com"> <title>Table listing var. 1</title> </head> <body> <?php $link=mysqli_connect('mysql.webzdarma.cz','zpp','[password]','zpp') or die('Error connecting database: '.mysqli_error($link) ); mysqli_real_query($link,'select * from test;'); $result=mysqli_use_result($link); echo "<table border=1>"; while ($row=mysqli_fetch_assoc($result)) { echo "<tr>"; foreach($row as $key => $value) {echo "<td>".$key."<td>".$value; }; } echo "</table>"; mysqli_close($link); ?> </body> </html> |
red - names of mysqli library function, blue - SQL command, green - parameters (which you have to adjust).
In this, php script:
Comment:
1. The $result variable is unreadable as standard php variable
(it is so called "handle"), so you have to use a
special function (in this case mysql_fetch_... function) to read
it.
2. Once you have read a line from the result using this function,
this data disapeares from the result. If you read last line, the
result become empty.
More common is to write the table headings only once. In this case, you can use following modification:
<!DOCTYPE
HTML PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=windows-1250"> <meta name="generator" content="PSPad editor, www.pspad.com"> <title>Table listing var 2.</title> </head> <body> <?php $link=mysqli_connect('mysql.webzdarma.cz','zpp','[password]','zpp') or die('Error connecting database: '.mysqli_error($link) ); mysqli_real_query($link,'select * from test;'); $result=mysqli_use_result($link); echo "<table border=1>"; if ($row=mysqli_fetch_assoc($result)) { echo "<tr>"; foreach($row as $key => $value) {echo "<th>".$key; }; echo "<tr>"; foreach($row as $key => $value) {echo "<td>".$value; }; } while ($row=mysqli_fetch_assoc($result)) { echo "<tr>"; foreach($row as $key => $value) {echo "<td>".$value; }; } echo "</table>"; mysqli_close($link); ?> </body> </html> |
Another modification is to use a form for allowing to use diffrent SQL command parameters. Because of mysqli_query returns data only with the SELECT command, we can create following php script modification:
<!DOCTYPE
HTML PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=windows-1250"> <title>Table listing var 3.</title> </head> <body> <?php $link=mysqli_connect('mysql.webzdarma.cz','zpp','[pwd]','zpp') or die('Error connecting database: '.mysqli_error($link) );
|
We should call this by a form like:
|
You can use this on the end of the previous php example, so it can call itself. But you should check, if any data has been sent.
Keep this solution - it could be part of an administrators' interface.