How to catch error and continue after a mysql error

I have the following code:

$squery = "SELECT TABLE_SCHEMA,TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME LIKE '%configuration_lang'";
$sres = dbquery($squery);
while ($srow = mysqli_fetch_array($sres)) {
  $vquery = 'SELECT value FROM `' . $srow["TABLE_SCHEMA"] . '`.' . $prefix . 'configuration WHERE name="PS_VERSION_DB"';
  try {
    $vres = @mysqli_query($conn, $vquery);
  } catch (Throwable $t) {
    continue;
  }
  $vrow = mysqli_fetch_array($vres);
  ...further processing
}

Some tables are defect and throw a 1932 error (“table doesn’t exist”) in the inner query. I try to ignore this with the catch structure.

However, what happens is that I will just get the same error with the next query ($srow=mysqli_fetch_array($sres)).

Why does this happen? And is there anything that I can do about it?

The issue lies in how MySQL handles the error when a query fails. When the inner query fails with the 1932 error (“Table doesn’t exist”), MySQL invalidates the database connection. After that, any subsequent query attempts using the same connection will also fail. This is why you encounter errors with the next query $srow = mysqli_fetch_array($sres).

Root Cause:

The invalidation of the connection happens because 1932 is a critical error. Although you tried to handle it with a try-catch, the MySQL connection is already unusable after such an error.

Solutions:

Solution 1: Validate Table Existence Before Querying

Before executing the inner query, check if the table exists using a safe query like this:

php

Copy code

$vquery = 'SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = "' . $srow["TABLE_SCHEMA"] . '" AND TABLE_NAME = "' . $prefix . 'configuration"';
$vres = mysqli_query($conn, $vquery);
if (mysqli_fetch_array($vres)[0] > 0) {
  $vquery = 'SELECT value FROM `' . $srow["TABLE_SCHEMA"] . '`.' . $prefix . 'configuration WHERE name="PS_VERSION_DB"';
  try {
    $vres = @mysqli_query($conn, $vquery);
  } catch (Throwable $t) {
    continue;
  }
  $vrow = mysqli_fetch_array($vres);
  // ... further processing
}

This approach ensures you query only valid tables.