Another way to check if a table exists:
if(mysql_num_rows(mysql_query("SHOW TABLES LIKE '".$table."'"))==1) echo "Table exists";
else echo "Table does not exist";
(PHP 4, PHP 5)
mysql_tablename — Retorna o nome da tabela do campo
$result
, int $i
) : stringmysql_tablename() usa um ponteiro de resultado retornado por mysql_list_tables() ou um índice inteiro e retorna o nome da tabela A função mysql_num_rows() pode ser usada para determinar o número de tabelas no ponteiro do resultado. Use a função mysql_tablename() para transpor o resultado, ou qualquer função para pegar o resultado como mysql_fetch_array().
Exemplo #1 Exemplo mysql_tablename()
<?php
mysql_connect("localhost", "mysql_user", "mysql_password");
$result = mysql_list_tables("mydb");
for ($i = 0; $i < mysql_num_rows($result); $i++) {
echo "Table: ", mysql_tablename($result, $i), "\n";
}
mysql_free_result($result);
?>
Veja também mysql_list_tables().
Another way to check if a table exists:
if(mysql_num_rows(mysql_query("SHOW TABLES LIKE '".$table."'"))==1) echo "Table exists";
else echo "Table does not exist";
A simple function to check for the existance of a table:
function TableExists($tablename, $db) {
// Get a list of tables contained within the database.
$result = mysql_list_tables($db);
$rcount = mysql_num_rows($result);
// Check each in list for a match.
for ($i=0;$i<$rcount;$i++) {
if (mysql_tablename($result, $i)==$tablename) return true;
}
return false;
}