PHP/mySQL:带有 SELECT、FROM 和 WHERE 的 mysql_query

发布于 2024-12-18 07:47:14 字数 747 浏览 1 评论 0原文

我正在尝试使用以下代码设置 $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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧伤还要旧人安 2024-12-25 07:47:15

您正在覆盖每个记录的变量,也许创建一个像这样的数组

 $data = array();
 while ($nextPageArray = mysql_fetch_assoc($fetchedNextPageData)) {
   $data[] = array('id' => $nextPageArray['pageID'], 
            'title' => $nextPageArray['title']);
 }

you are overwriting the variable for each record, maybe create an array like

 $data = array();
 while ($nextPageArray = mysql_fetch_assoc($fetchedNextPageData)) {
   $data[] = array('id' => $nextPageArray['pageID'], 
            'title' => $nextPageArray['title']);
 }
天涯沦落人 2024-12-25 07:47:15

现在忘记 PHP。这是您的 SQL 查询:

SELECT pageID, pageCategoryID, 'order', title 
FROM pages 
WHERE pageCategoryID='1' AND 'order'='2'

在 SQL 中,与许多其他语言一样,您使用引号来键入文字字符串。由于 'order' 字符串永远不会等于 '1' 字符串,因此无论其他值如何,您的查询将始终返回零行。

如果 order 是列名,则不能用单引号将其引起来。

现在,鉴于 ORDER保留的单词,你必须在它周围使用反引号。您还可以输入整数作为整数(无需引用它们):

SELECT pageID, pageCategoryID, `order`, title 
FROM pages 
WHERE pageCategoryID=1 AND `order`=2

Forget about PHP right now. This is your SQL query:

SELECT pageID, pageCategoryID, 'order', title 
FROM pages 
WHERE pageCategoryID='1' AND 'order'='2'

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):

SELECT pageID, pageCategoryID, `order`, title 
FROM pages 
WHERE pageCategoryID=1 AND `order`=2
睫毛溺水了 2024-12-25 07:47:15

在查询的 WHERE 子句中,尝试删除 order 周围的引号,并使用反引号代替,如下所示:

`order`='$nextPageOrder'

并尽量避免使用表/列名称的关键字!去过那里。

In the WHERE clause of your query try removing the quotes around order and use back ticks instead like this:

`order`='$nextPageOrder'

And try to avoid using keywords for table/column names! Been there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文