echo从数据库中创建的最后一个表名。在这里,我无法回应这个名字

时间:2014-12-28 10:52:32

标签: php mysql

我收到警告 - > mysql_fetch_array()期望参数1为resource,boolean

enter code here
$db // database name
$sql1="SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND       TABLE_SCHEMA=".$db." order by create_time desc limit 1";
$res1 = mysql_query($sql1);
if($row = mysql_fetch_array($res1))
echo $row['TABLE_NAME'];
else echo "unable to fetch table name";

1 个答案:

答案 0 :(得分:2)

您需要在$db变量的包含周围加上引号。改变这一行:

$sql1="SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND       TABLE_SCHEMA=".$db." order by create_time desc limit 1";

要:

$sql1="SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND       TABLE_SCHEMA='".$db."' order by create_time desc limit 1";

请注意mysql_* functions are deprecated。使用PDOmysqli_*prepared statements一起使用可以避免申请中出现security risks