I'm trying to put the tables from MySQL database in a HTML page using PHP.
I'm beginner in PHP and I face a problem to the mysqli_query
function.
This is my PHP code:
// connect to the db
$host = 'localhost';
$user = 'root';
$pass = '';
$db = 'testdb';
$connection = mysqli_connect($host, $user, $pass, $db) or die("cannot connect to db");
// show the tables
$result = mysqli_query($connection, 'SHOW TABLES') or die ('cannot show tables');
while($tableName = mysqli_fetch_row($result)) {
$table = $tableName[0];
echo '<h3>', $table, '</h3>';
$result2 = mysqli_query($table, 'SHOW COLUMNS FROM') or die("cannot show columns");
if(mysqli_num_rows($result2)) {
echo '<table cellpadding = "0" cellspacing = "0" class "db-table">';
echo '<tr><th>Field</th><th>Type</th><th>Null</th><th>Key</th><th>Default</th><th>Extra</th></tr>';
while($row2 = mysqli_fetch_row($result2)) {
echo '<tr>';
foreach ($row2 as $key=>$value) {
echo '<td>',$value, '</td>';
}
echo '</tr>';
}
echo '</table><br />';
}
}
Unfortunately I get this error:
Warning: mysqli_query() expects parameter 1 to be mysqli, string given in C:\xampp\htdocs\test\showtables.php on line 26, cannot show columns.
I've also tried with the mysql_query
function, but I faced another error, I then I changed it back to the mysqli_query
function.
This code show you all of the tables and tables rows. I try it works
Sample Output :
In the second call to the
mysqli_query
function, you're passing in the table name instead of the connection. Try something like this:$result2 = mysqli_query($connection, "SHOW COLUMNS FROM $table") or die("cannot show columns");
http://php.net/manual/en/mysqli.query.php