-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();
                }
4
  • Possible duplicate of Check if a database table exists using PHP/PDO Commented Mar 21, 2019 at 11:42
  • @GugaNemsitsveridze Did u at least waste a second viewing my question? Commented Mar 21, 2019 at 11:50
  • 1
    please read minimal reproducible example Commented Mar 21, 2019 at 11:56
  • The query, as currently written, is invalid. You have no placeholder so $DataDeConsulta has nothing to bind to. Use a try/catch. Commented Mar 21, 2019 at 12:27

2 Answers 2

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';
Sign up to request clarification or add additional context in comments.

1 Comment

My issue isn't with the mysql syntax
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;
}

4 Comments

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 You've made no mention anywhere of you actually researching, before posting this question
@KebabProgrammer and no one asked if I've already researched before marking as duplicated.. it was just to earn points
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.