0
/**
 * 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;
}
  1. 如何使用 Idiorm PDO 配置此功能?
  2. 可以使用吗-

    try {
        $page = ORM::for_table($table)->where('slug', $slug )->find_one();  
    } (catch $e) {
        // 404 with an error that table does not exists.
    }
    

而不是“tableExists”函数?

4

1 回答 1

0

如果我正确理解您的问题,您想检查 MySQL 数据库中是否存在表。

问:1

需要注意的是,这两个查询并不相同。你有:

$page = ORM::for_table($table)->where('slug', $slug )->find_one();

但是您的第二个查询应该是:

$page = ORM::for_table($table)->select_expr(1)->find_one();

有关信息,请参阅结果列文档

问2

是的,Idiorm 在下面使用 PDO,所以你会得到相同的 PDO 异常——你真的可以尝试看看它是如何工作的:

try {
    $page = ORM::for_table($table)->where('slug', $slug )->find_one();  
} (catch $e) {
    var_dump($e);
}
于 2017-03-21T23:30:12.797 回答