如果表不存在则创建错误消息

时间:2019-03-26 10:50:33

标签: php pdo

我尝试了一些研究,但没有发现与我的问题类似的东西。
我发现确实有很多检查表是否存在的方法,但是我想我的问题是我的表具有纯文本和类似Opened_". $DataDeConsulta ."的变量
我尝试了以下代码,但可能会丢失一些内容。有人可以帮我吗?预先感谢!

if ($valid) {
    $pdo = Database::connect();
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $stmt = $pdo->prepare("Select 1 from `Opened_". $DataDeConsulta ."`");
    $stmt->execute(["Opened_". $DataDeConsulta .""]);
    $count = $stmt->fetchColumn();
    if ($count <= 0) {
        $msg="No Data Found";
        echo($msg);
    }
    else{
    $sql = "SELECT * FROM filecleaner.`Opened_". $DataDeConsulta ."`";
    $_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();
    }
}

$Valid设置在另一页上(只是一个错误验证)

1 个答案:

答案 0 :(得分:0)

好吧,我找到了一种可以对某人有用的解决方案:

                if (isset($valid)) {
                $pdo = Database::connect();
                $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                $stmt = $pdo->prepare("SELECT COUNT(*) FROM information_schema.tables  WHERE table_schema = 'filecleaner'  AND table_name = 'Opened_". $DataDeConsulta ."'");
                $stmt->execute(["Opened_". $DataDeConsulta .""]);
                $count = $stmt->fetchColumn();
                if ($count <= 0) {
                    $msg="No Data Found";
                    echo($msg);
                }
                else{
                $sql = "SELECT * FROM filecleaner.`Opened_". $DataDeConsulta ."`";
                $_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();
                }
            }