【问题标题】:php mysql validate if table exist using pdophp mysql使用pdo验证表是否存在
【发布时间】:2019-08-12 06:08:53
【问题描述】:

我有以下代码来查看表(基于用户选择)是否存在,但它给了我以下错误:

[21-Mar-2019 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->执行(数组) 1 {main} 在第 126 行的 C:\inetpub\wwwroot\FileCleaner\consultas.php 中抛出

                 $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();
                }

【问题讨论】:

标签: php mysql pdo


【解决方案1】:

你可以使用这个select来查看mysql/mariadb中是否存在表:

SELECT * FROM information_schema.tables WHERE table_schema = 'you-database-name' AND table_name = 'your-table-name';

【讨论】:

  • 我的问题不在于 mysql 语法
【解决方案2】:

您是否尝试过使用这种 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;
}

【讨论】:

  • 正如我所说,您应该浪费一些时间查看问题 jhez,我已经先在 google 和 StackOverflow 上搜索,我已经尝试过了!
  • @Mig 在发布此问题之前,您没有提及您实际研究的任何地方
  • @KebabProgrammer 没有人问我在标记为重复之前是否已经研究过......这只是为了赚取积分
  • 仅供参考,将问题标记为重复可以帮助您找到与您遇到的问题很接近的问题,而不是因为他们试图让人讨厌。如果你做过研究,在你的问题中说这是你做过的研究,添加你去过的地方的链接,这样你就能得到最好的帮助,而不是嘲笑那些花时间回答你问题的人
猜你喜欢
  • 2018-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-24
  • 2021-01-24
相关资源
最近更新 更多