PHP/mySQL:带有 SELECT、FROM 和 WHERE 的 mysql_query
我正在尝试使用以下代码设置 $nextPageID
和 $nextPageTitle
。不幸的是,他们还没有做好准备。
$pageCategoryID = 1;
$nextPageOrder = 2;
$fetchedNextPageData = mysql_query("
SELECT pageID, pageCategoryID, 'order', title
FROM pages
WHERE pageCategoryID='$pageCategoryID' AND 'order'='$nextPageOrder'
") or die(mysql_error());
while ($nextPageArray = mysql_fetch_array($fetchedNextPageData)) {
$nextPageID = $nextPageArray['pageID'];
$nextPageTitle = $nextPageArray['title'];
}
我的表 pages
包含一行 pageID = 2、pageCategoryID = 1、order = 2、title = Next Page 以及其他 4 列,其中包含此查询不需要的数据。
出于测试目的,该代码已被简化。当我让它工作后,它会被消毒。
有什么想法可以让这段代码正常工作吗?
I am trying to set $nextPageID
and $nextPageTitle
with the following code. Unfortunately they are not getting set.
$pageCategoryID = 1;
$nextPageOrder = 2;
$fetchedNextPageData = mysql_query("
SELECT pageID, pageCategoryID, 'order', title
FROM pages
WHERE pageCategoryID='$pageCategoryID' AND 'order'='$nextPageOrder'
") or die(mysql_error());
while ($nextPageArray = mysql_fetch_array($fetchedNextPageData)) {
$nextPageID = $nextPageArray['pageID'];
$nextPageTitle = $nextPageArray['title'];
}
My table pages
contains a row with pageID = 2, pageCategoryID = 1, order = 2, title = Next Page, plus 4 other columns with data I don't need for this query.
This code has been simplified for testing purposes. It will be sanitized after I get it working.
Any thoughts on what I can do to get this bit of code working?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在覆盖每个记录的变量,也许创建一个像这样的数组
you are overwriting the variable for each record, maybe create an array like
现在忘记 PHP。这是您的 SQL 查询:
在 SQL 中,与许多其他语言一样,您使用引号来键入文字字符串。由于
'order'
字符串永远不会等于'1
' 字符串,因此无论其他值如何,您的查询将始终返回零行。如果
order
是列名,则不能用单引号将其引起来。现在,鉴于
ORDER
是 保留的单词,你必须在它周围使用反引号。您还可以输入整数作为整数(无需引用它们):Forget about PHP right now. This is your SQL query:
In SQL, as in many other languages, you use quotes to type literal strings. Since the
'order'
string will never equal the'1
' string, your query will always return zero rows, no matter the other values.If
order
is a column name, you cannot single-quote it.Now, given that
ORDER
is a reserved word, you'll have to use backticks around it. You can also type integers as integers (there's no need to quote them):在查询的 WHERE 子句中,尝试删除 order 周围的引号,并使用反引号代替,如下所示:
并尽量避免使用表/列名称的关键字!去过那里。
In the WHERE clause of your query try removing the quotes around order and use back ticks instead like this:
And try to avoid using keywords for table/column names! Been there.