我有以下代码来查看表(基于用户选择)是否存在,但这给了我以下错误:
[2019年3月21日11:34:11 UTC] PHP致命错误:未捕获的PDOException: SQLSTATE [42S02]:找不到基表或视图:1146表 'filecleaner.opened_2019-03-21'在以下位置不存在 C:\ inetpub \ wwwroot \ FileCleaner \ consultas.php:126堆栈跟踪: 0 C:\ inetpub \ wwwroot \ FileCleaner \ consultas.php(126):PDOStatement-> execute(Array) 在第126行的C:\ inetpub \ wwwroot \ FileCleaner \ consultas.php中抛出1个{main}
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $pdo->prepare("SELECT * FROM filecleaner.`Opened_". $DataDeConsulta ."`");
$stmt->execute([$DataDeConsulta]);
$count = $stmt->fetchColumn();
if ($count <= 0) {
$DataDeConsultaError = 'There is no information on that date!';
$valid = false;
}
if (isset($valid)) {
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT * FROM filecleaner.`Opened_". $DataDeConsulta ."`";
//session_start();
$_SESSION['DataDeConsulta'] = $DataDeConsulta;
$query_result=$pdo->query($sql);
foreach ($pdo->query($sql) as $row) {
echo '<tr>';
echo '<td>'. htmlentities($row['Emails']) . '</td>';
echo ' ';
echo '</td>';
echo '</tr>';
}
Database::disconnect();
}
答案 0 :(得分:0)
您可以使用此选择来查看表在mysql / mariadb中是否存在:
SELECT * FROM information_schema.tables WHERE table_schema = 'you-database-name' AND table_name = 'your-table-name';
答案 1 :(得分:0)
您是否尝试过使用try-catch语法?
/**
* 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;
}