如果数据库不为空,我需要检查300多个数据库,并显示没有表格的数据库名称。
这是我的代码[编辑]:
<?php
$db = ['db_1', 'db_2', 'db_3', 'db_4'];
foreach ($db as $db_name) {
$conn = mysqli_connect("host", "user", "pass", $db_name) or die("Error " . mysqli_error($conn));
}
if ($conn->connect_error) {
die("Conect error: " . $conn->connect_error);
}
$sql = "SHOW TABLES FROM " . $db_name . "";
$result = mysql_query($sql);
if ($result == NULL) {
foreach ($db as $db_name) {
echo "Empty database:" . $db_name . "</br>";
}
} else {
exit;
}
?>
但代码返回所有数据库,
答案 0 :(得分:2)
这应该适合你:
<?php
$db = ['db_1', 'db_2', 'db_3', 'db_4'];
$conn = mysqli_connect("host", "user", "pass") or die("Error connecting: " . mysqli_error($conn));
foreach ($db as $db_name) {
$sql = "SHOW TABLES FROM " . $db_name;
$result = mysqli_query($conn, $sql);
if (!$result) {
echo "Unable to get tables from:" . $db_name . "<br />";
} else {
if ( mysqli_num_rows($result) === 0 )
echo "Empty database:" . $db_name . "</br>";
}
}
mysqli_close($conn);
?>
旁注:
如果您的PHP版本不支持数组的方括号,请尝试更改:
$db = ['db_1', 'db_2', 'db_3', 'db_4'];
到
$db = array('db_1', 'db_2', 'db_3', 'db_4');