0

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

即使我输入一个退出的表,它也会显示Not Exists: Incorrect table name

$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

4 回答 4

1
if(mysql_num_rows(mysql_query("SHOW TABLES LIKE '".$table."'"))==1) 
    echo "Table exists";
else echo "Table does not exist";
于 2013-09-26T17:15:44.630 回答
1
$sql1=MYSQL_QUERY('SELECT count(name) FROM "'.$mainnamechk.'" LIMIT 1');

if(mysql_num_rows($sql1)==0)
{
 echo 'Not exists';
}
else
{
echo 'Exists';
}
于 2013-09-26T17:19:35.790 回答
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
于 2013-09-26T17:15:41.003 回答
0

利用INFORMATION_SCHEMA

SELECT * FROM information_schema.tables
    WHERE table_schema = 'database name' 
    AND table_name = 'table_name'
LIMIT 1;
于 2013-09-26T17:16:55.980 回答