PHP sql 查询不返回任何内容 [英] PHP sql query isn't returning anything
问题描述
以下 PHP 代码:
$dbc = mysql_connect ('localhost','root','abcde');
if (!$dbc) {
die('Not Connected:' . mysql_error ());
}
$db_selected = mysql_select_db ('db_test', $dbc);
if (!$db_selected) {
die ("Can't Connect :" .mysql_error());
}
$query="SELECT * FROM 140423 WHERE GAME='Clash of Clans'";
//add "result"
$result=mysql_query($query);
if (!$query) {
die ("Can't Connect :" .mysql_error());
}
echo $result;
不返回任何东西.
我应该使用打印而不是回显吗?
Should I be using print instead of echo?
另外,如果我改变了
echo $result;
到
echo 'whatever';
它会在我的帖子中返回whatever".
it returns "whatever" on my post.
帮助?
推荐答案
来自手册:
标识符可以以数字开头,但除非引用,否则不能仅由数字组成.
- http://dev.mysql.com/doc/refman/5.1/en/identifiers.html
为您的表选择一个不同的名称,最好由字母组成.
Choose a different name for your table, one consisting of letters preferably.
这正是你正在做的,不应该做的.
Which is exactly what you're doing, and should not be doing.
解决方案:
在表名周围使用反引号.
Use backticks around the table name.
另外,如果你想查看数据,你需要使用循环.
Plus, if you wish to view data, you need to use a loop.
即:
$query="SELECT * FROM `140423` WHERE GAME='Clash of Clans'";
while ($row = mysql_fetch_assoc($query))
{
$var = $row["column"]; // the column of your choice
}
echo $var;
<小时>
重写:
$dbc = mysql_connect ('localhost','root','abcde');
if (!$dbc) {
die('Not Connected:' . mysql_error ());
}
$db_selected = mysql_select_db ('db_test', $dbc);
if (!$db_selected) {
die ("Can't Connect :" .mysql_error());
}
$query="SELECT * FROM `140423` WHERE GAME='Clash of Clans'";
//add "result"
$result=mysql_query($query);
// as pointed out already
if (!$result) {
die ("Can't Connect :" .mysql_error());
}
echo $result; // this is up to you
// if you want to see data from your table, loop through it.
while ($row = mysql_fetch_assoc($query))
{
$var = $row["column"]; // the column of your choice
}
echo $var;
<小时>
脚注:
mysql_*
函数弃用通知:
http://www.php.net/manual/en/intro.mysql.php
自 PHP 5.5.0 起此扩展已弃用,不推荐用于编写新代码,因为将来会删除它.相反,mysqli 或
This extension is deprecated as of PHP 5.5.0, and is not recommended for writing new code as it will be removed in the future. Instead, either the mysqli or PDO_MySQL extension should be used. See also the MySQL API Overview for further help while choosing a MySQL API.
这些函数允许您访问 MySQL 数据库服务器.有关 MySQL 的更多信息,请访问 » http://www.mysql.com/.
These functions allow you to access MySQL database servers. More information about MySQL can be found at » http://www.mysql.com/.
MySQL 文档可在 » http://dev.mysql.com/doc/.
Documentation for MySQL can be found at » http://dev.mysql.com/doc/.
这篇关于PHP sql 查询不返回任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!