不能够查询在MySQL数据库中的表,mysqli_query返回false
问题描述:
我刚开始学习PHP,我想设置连接到本地MySQL分区 我已经粘贴了我试图运行的代码。不能够查询在MySQL数据库中的表,mysqli_query返回false
我得到$ result = false 我到达其他部分 - “不返回” 在附图中,在db test下有一个名为“table”的表,我也有3-4个条目
有人能花时间帮我明白怎么回事错在这里?
谢谢!
<!doctype html>
<html>
<head>
</head>
<body>
<?php
$link = mysqli_connect('localhost','root','','test');
$query = "select * from 'table' " ;
$outputDisplay = "";
$myrowcount = 0;
if (!$link)
{
die('Could not connect to MySQL: ' . mysqli_error());
} else
{
echo 'Connection OK';
print ($query);
$result = mysqli_query($link, $query);
if($result)
{
$numresults = mysqli_num_rows($result);
printf("Select returned %d rows.\n", $numresults);
}
else
{
echo "No return";
}
mysqli_close($link);
}
?>
</body>
</html>
答
mysqli_query()
返回false,因为一些错误发生在你的query..Here你的问题是用单引号的单quotes..Instead你应该用背蜱喜欢
$query = "select * from `table` " ;
答
$query = "select * from 'table' " ;
你不能把单引号的表名。单引号用于字符串文字和日期文字。
你可以把在反单引号表名称:
$query = "select * from `table` " ;
谢谢您的回答,看完这个,我搜索一点关于背蜱,所以现在我的理解时,表名或列名是一个MySQL的保留字,才需要反引号。我对吗 ??还是有更多的东西呢? ..我需要重新打勾,因为我的表名是“表”? – 2014-11-25 03:24:20
@swatisaoji是的,你发现是正确的。当有一个保留的工作作为列或表名称时,使用backticks .. :) – 2014-11-25 13:34:06