我正在尝试在我的数据库中显示所有表名。这是我的代码:
<?php
define('DB_HOST', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_NAME', 'nucleus');
$db=new mysqli(DB_HOST,DB_USERNAME,DB_PASSWORD,DB_NAME);
$query = $db->prepare('SHOW TABLES FROM `nucleus`');
$query->execute();
while($rows = $query->fetch()){
echo " $rows";
}
?>
每个条目都显示1
,我不知道为什么。对此有何解决方案?
答案 0 :(得分:1)
所以,任何寻找可能答案的人,都是:
<?php
$query = $db->prepare('SHOW TABLES FROM `databasename`');
$query->execute();
$query->bind_result($tables);
while( $query->fetch()){
echo " $tables";
}
?>
答案 1 :(得分:0)
要获取所有表的名称,请使用:
SELECT table_name FROM information_schema.tables;
要从特定数据库获取表的名称,请使用:
SELECT table_name FROM information_schema.tables where table_schema='<your_database_name>';
有关详细信息,请参阅:http://dev.mysql.com/doc/refman/5.0/en/information-schema.html
答案 2 :(得分:0)
每个条目都显示1,我不知道为什么
fetch()返回一个布尔值,因此返回1
的结果。每次找到一行时都会返回true
。
您需要使用bind_result()将结果绑定到您可以为每行引用的变量。
$stmt->prepare("SELECT table_name FROM information_schema.tables where table_schema=?")
$stmt->bind_param('s', 'YOUR_DB_NAME');
$stmt->execute();
$stmt->bind_result($table);
$stmt->store_result();
while($stmt->fetch()) {
echo $table;
}
$stmt->close();
答案 3 :(得分:0)
我做了一个完整的解决方案,有两种方法可以获得SQL结果,我首选获得结果的优先级,$ ROW将接收数组数据显示:
<?php
$connection=conectadb();
$sql = 'SHOW TABLES FROM pmsai';
$result = $connection->query($sql);
if (!$result) {
echo "DB Error, could not list tables\n";
echo 'MySQL Error: ' . mysqli_error();
exit;
}
while ($row = $result->fetch_array()) {
echo "<p>Table: {$row[0]}</p>";
}
mysqli_close($connection);
?>
答案 4 :(得分:0)
SHOW TABLE STATUS
如果已经建立连接,则无需提及数据库名称