表名用PHP检查可用性

时间:2013-09-26 17:12:43

标签: php mysql

我正在尝试使用php脚本创建一个表。在此之前,我试图用PHP检查可用的名称。但它没有用。

即使我输入退出的表格显示不存在:表名不正确

$sql1 = "SELECT * FROM `$mainnamechk`;";

if (mysql_query($sql1))
{
echo "<img width='35' height='25' src='img/good.png' title='Database $mainnamechk Exists'>";
  }
else
  {
   echo "Not Exists:" . mysql_error();
  echo"<br>";

  }

4 个答案:

答案 0 :(得分:1)

if(mysql_num_rows(mysql_query("SHOW TABLES LIKE '".$table."'"))==1) 
    echo "Table exists";
else echo "Table does not exist";

答案 1 :(得分:1)

$sql1=MYSQL_QUERY('SELECT count(name) FROM "'.$mainnamechk.'" LIMIT 1');

if(mysql_num_rows($sql1)==0)
{
 echo 'Not exists';
}
else
{
echo 'Exists';
}

答案 2 :(得分:0)

尝试检查information_schema

SELECT count(*) FROM information_schema.TABLES
WHERE (TABLE_SCHEMA = '" . $database . "') AND (TABLE_NAME = '" . $tablename . "');

如果存在则返回1,如果不存在,则返回0.

$database = "databasename";
$table = "tablename";
$con = new mysqli(/* info */);
$result = mysqli_query($con, "SELECT count(*) FROM information_schema.TABLES WHERE (TABLE_SCHEMA = '" . $database . "') AND (TABLE_NAME = '" . $table . "'");
// $result == 1 or 0

答案 3 :(得分:0)

使用INFORMATION_SCHEMA

SELECT * FROM information_schema.tables
    WHERE table_schema = 'database name' 
    AND table_name = 'table_name'
LIMIT 1;