EDIT: I had a typo in my original post....the issue is a bit more complicated...i had a variable passed in not a raw string.
我想打印一个特定于某个人的mysql数据库中的故事: 所以我的代码类似于:
$stuff ="jamie"
$query = "SELECT * FROM person_stories WHERE person =$stuff";
$result = mysql_query($query) or die ("didnt work");
while($row = mysql_fetch_array($result))
{
echo "<a href = 'PersonStoryPage.php?pid=$row[id]'>" .$row['title']. " </a>";
}
我继续“没有工作”......我知道我的表person_stories
是空的,但这与错误相同吗?该表显然不会总是为空,因此我需要能够使用这段代码来开展业务。
非常感谢帮助!
编辑2:实际错误是:
Unknown column 'jamie' in 'where clause'
这很奇怪,因为它不应该将jamie
解释为列!
答案 0 :(得分:4)
您没有在jamie
附近放置单引号。试试这个:
$query = "SELECT * FROM person_stories WHERE person = 'jamie'"
我看到该帖子已被编辑。它现在应该改变:
$stuff ="jamie"
$query = "SELECT * FROM person_stories WHERE person =$stuff";
这样的事情:
$stuff ="jamie"
$query = "SELECT * FROM person_stories WHERE person='" . mysql_real_escape_string($stuff) . "'";
这不仅可以解决您的SQL语法错误,还可以保护您的应用免受令人讨厌的SQL注入漏洞。
答案 1 :(得分:3)
您需要引用person字段的条件值(注意jamie周围的撇号):
$query = "SELECT * FROM person_stories WHERE person = 'jamie'";
修改强>
已更新以符合您的更新。如果您使用可能来自用户输入的变量,那么您将需要使用mysql_real_escape_string
来正确地转义SQL查询的值(有助于防止SQL注入)。
$query = "SELECT * FROM person_stories WHERE person = '" . mysql_real_escape_string($stuff) . "'";
答案 2 :(得分:3)
如果您与字符串进行比较,则必须用''
将其括起来尝试更改
$query = "SELECT * FROM person_stories WHERE person =jamie";
到
$query = "SELECT * FROM person_stories WHERE person = 'jamie'";
在我看来,最好修改这一行:
echo "<a href = 'PersonStoryPage.php?pid=$row[id]'>" .$row['title']. " </a>";
到
echo "<a href = 'PersonStoryPage.php?pid=".$row['id']."'>" .$row['title']. " </a>";
答案 3 :(得分:0)
您的查询中有错误。你缺少双引号新jamie 试试这个
$query = 'SELECT * FROM person_stories WHERE person ="jamie"';