/**
* 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;
}
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.
It should be noted that the two queries are not identical. You've got:
$page = ORM::for_table($table)->where('slug', $slug )->find_one();
But your second query should be:
$page = ORM::for_table($table)->select_expr(1)->find_one();
See the result columns documentation for information.
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:
try {
$page = ORM::for_table($table)->where('slug', $slug )->find_one();
} (catch $e) {
var_dump($e);
}