/**
* Check if a table exists in the current database.
*
* @param PDO $pdo PDO instance connected to a database.
* @param string $table Table to search for.
* @return bool TRUE if table exists, FALSE if no table found.
*/
function tableExists($pdo, $table) {
// Try a select statement against the table
// Run it in try/catch in case PDO is in ERRMODE_EXCEPTION.
try {
$result = $pdo->query("SELECT 1 FROM $table LIMIT 1");
} catch (Exception $e) {
// We got an exception == table not found
return FALSE;
}
// Result is either boolean FALSE (no table found) or PDOStatement Object (table found)
return $result !== FALSE;
}
- How to configure this function with Idiorm PDO?
Is it okey to use -
try { $page = ORM::for_table($table)->where('slug', $slug )->find_one(); } (catch $e) { // 404 with an error that table does not exists. }
instead of the "tableExists" function?
If I am understanding your question correctly you want to check if a table exists in a MySQL database.
Q. 1
It should be noted that the two queries are not identical. You've got:
But your second query should be:
See the result columns documentation for information.
Q. 2
Yes, Idiorm uses PDO underneath so you'll get the same PDO exception - this is something you really could just try and see how it works with: