Connecting to 'information_schema'
$dbHost = '';
$dbUser = '';
$dbPwd = '';
$dbDb = 'information_schema';
$dbTable = 'myTable';
$conn = new mysqli($dbHost, $dbUser, $dbPwd, $dbDb);
$sql = 'SELECT TABLE_NAME FROM TABLES WHERE TABLE_NAME = ?';
if (!$stmt = $conn->prepare($sql)) {
echo sprintf('MySQL error message: %s<br />',$conn->error);
}
$stmt->bind_param('s', $dbTable);
$stmt->execute();
$stmt->bind_result($bla);
if ($stmt->fetch()) {
echo 'table found';
} else {
echo 'table not found';
}
This works...
...as well as retrieving all tables and comparing the table name to the result:
$dbHost = '';
$dbUser = '';
$dbPwd = '';
$dbDb = 'myDB';
$dbTable = 'myTable';
$conn = new mysqli($dbHost, $dbUser, $dbPwd, $dbDb);
$rst = $conn->query('SHOW TABLES');
$result = 'table not found';
while ($row = $rst->fetch_row()) {
if (in_array($dbTable, $row, true)) {
$result = 'table found';
break;
}
}
echo $result;
The difference is:
In the first case I need a different connection (to the database 'information_schema').
In the second case I need to retrieve all the table names.
Both of which I would have liked to avoid.
Version 1 seems to be the better, right?
Thanks for delving into that 🙂
Bjom