-2

I have the following code to see if a table (based on the user selection) exist or not, but it's giving me the following error:

[21-Mar-2019 11:34:11 UTC] PHP Fatal error: Uncaught PDOException: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'filecleaner.opened_2019-03-21' doesn't exist in C:\inetpub\wwwroot\FileCleaner\consultas.php:126 Stack trace: 0 C:\inetpub\wwwroot\FileCleaner\consultas.php(126): PDOStatement->execute(Array) 1 {main} thrown in C:\inetpub\wwwroot\FileCleaner\consultas.php on line 126

                 $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();
                }
Mig
  • 59
  • 6

2 Answers2

0

You can use this select to see if table exists or not in mysql/mariadb:

SELECT * FROM information_schema.tables WHERE table_schema = 'you-database-name' AND table_name = 'your-table-name';
Petr
  • 1,159
  • 10
  • 20
0

Have you tried to use this try-catch syntax?

/**
* 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;
}
Guga Nemsitsveridze
  • 721
  • 2
  • 7
  • 27
  • As I said, you should waste some time viewing the question jhez, I've search first on google and StackOverflow, I've already tried that! – Mig Mar 21 '19 at 11:55
  • 1
    @Mig You've made no mention anywhere of you actually researching, before posting this question – Kebab Programmer Mar 21 '19 at 12:01
  • @KebabProgrammer and no one asked if I've already researched before marking as duplicated.. it was just to earn points – Mig Mar 21 '19 at 12:06
  • 3
    FYI, marking a question duplicate makes it so that its helps you find a question remotely close to the problem you are having, not because they are trying to be annoying. If you have done research, say it in your question that this is the research you've done, add links to where you've gone, so that you can get the best help, not ridicule ppl that took time to answer your question – Kebab Programmer Mar 21 '19 at 12:15