【发布时间】:2016-06-13 16:31:27
【问题描述】:
我正在尝试使用 PHP 将 MySQL 数据库中的表放入 HTML 页面中。
我是 PHP 初学者,我遇到了 mysqli_query 函数的问题。
这是我的 PHP 代码:
// 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 />';
}
}
不幸的是,我收到了这个错误:
警告:mysqli_query() 期望参数 1 为 mysqli,字符串在 C:\xampp\htdocs\test\showtables.php 在第 26 行,无法显示列。
我也尝试过使用mysql_query 函数,但遇到另一个错误,然后我将其改回mysqli_query 函数。
【问题讨论】:
-
请阅读
mysqli手册。特别是函数参数的一部分。 -
跟随你的第一次使用,
mysqli_query($connection,.. 这是手册:php.net/manual/en/mysqli.query.php。具体来说:mixed mysqli_query ( mysqli $link , string $query [, int $resultmode = MYSQLI_STORE_RESULT ] ). -
测试时代码对我来说工作正常。你的连接是否正确?检查数据库名称和其他东西..