One of our zen cart website throw out an error after system crashed.The error message is something as below:
abc.configuration does not exist (abc is the db name)
i logged into mysql console with
mysql -u root -p
then i issue those command in the mysql console
use abc;
show tables;
The configuration table is showed in the result list.When I browse to the phpmyadmin page,i found the configuration is missing.This make me confused.I thought the configuration table may be corrupted.so it’s not shown in phpmyadmin and any sql query on it will result in error.that’s why the zen cart website got 404 error page.
I usually check the table existence with following php function which based upon ‘show table’ query.
function table_exists($link,$db,$table) {
if (empty($table)) {
die('No table name provided to check');
}
$tables = array();
$sql = "SHOW TABLES FROM $db";
$result = mysql_query($sql, $link);
if (!$result) {
echo 'DB Error, could not list tables,' . mysql_error();
exit;
}
while ($row = mysql_fetch_row($result)) {
$tables[] = $row[0];
}
return in_array('configuration', $tables);
}
it is obviously that the ‘show tables’ query will lead to error when a table is corrupted.so is there better way to check?After some seconds i got solution from google.Below is the php function to check the existence of a table.It worked upon MySQL information(the information_schema database).
function table_exists($tablename, $link, $database = false) {
if(!$database) {
$res = mysql_query("SELECT DATABASE()", $link);
$database = mysql_result($res, 0);
}
$res = mysql_query("SELECT COUNT(*) AS count FROM information_schema.tables WHERE table_schema = '$database' AND table_name = '$tablename'", $link);
return mysql_result($res, 0) == 1;
}
i thought this would be better way to go.