我正确的 SQL 查询(在 phpmyadmin 上测试)在 PHP 中返回错误的结果

发布于 2024-10-17 11:42:41 字数 2988 浏览 1 评论 0原文

让我解释一下。我有一个简单的查询,当我在 phpmyadmin 上测试它时它可以工作,但它不会在我的网络应用程序中返回正确的答案。

这是运行查询的函数:

function GetProductsFromCategoryId($categoryId)
{
    global $db;
    $query = '
        SELECT *
        FROM `products`
        WHERE categoryID = 3';
    try {
        $statement = $db->prepare($query);
        //$statement->bindValue(':categoryId', $categoryId);
        $statement->execute();
        $result = $statement->fetch();
        $statement->closeCursor();
        return $result;
    } catch (PDOException $e) {
        $error_message = $e->getMessage();
        display_db_error($error_message);
    }
}

通常是categoryID = $categoryID,但我用“3”对其进行硬编码,因为我想查看结果。还有整个其他数据库文件来处理连接,如果您愿意,我可以发布它们,但其他功能工作正常。这是我使用 Murach 的 php 书第 24 章中的模板。这个函数是我自己的,稍作修改。

使用 SELECT * 时,该函数应该为每个产品返回 2 个数组,稍后当我从 phpmyadmin 发布结果时您将看到这一点。相反,它只为一种产品返回 1 个数组。

这是我在 phpmyadmin 中运行的查询:

SELECT * FROM `products` WHERE categoryID=3;

结果是:

        9   3   ludwig  Ludwig 5-piece Drum Set with Cymbals    This product includes a Ludwig 5-piece drum set an...   699.99  30.00   2010-07-30 12:46:40
        10  3   tama    Tama 5-Piece Drum Set with Cymbals  The Tama 5-piece Drum Set is the most affordable T...   799.99  15.00   2010-07-30 13:14:15

因此,如您所见,它应该返回 2 个数组,因为这就是 fetch() 函数应该执行的操作,但它只返回一个数组。我用netbeans设置了xdebug并查看变量$result,它有1个带有productName“Ludwig 5-piece...”的数组

我已经坚持了3天,我不知道为什么它不起作用。请帮忙!

谢谢!!!

PS:这是数据库表上的产品,

        1   1   strat   Fender Stratocaster The Fender Stratocaster is the electric guitar des...   699.00  30.00   2009-10-30 09:32:40
        2   1   les_paul    Gibson Les Paul This Les Paul guitar offers a carved top and humbu...   1199.00 30.00   2009-12-05 16:33:13
        3   1   sg  Gibson SG   This Gibson SG electric guitar takes the best of t...   2517.00 52.00   2010-02-04 11:04:31
        4   1   fg700s  Yamaha FG700S   The Yamaha FG700S solid top acoustic guitar has th...   489.99  38.00   2010-06-01 11:12:59
        5   1   washburn    Washburn D10S   The Washburn D10S acoustic guitar is superbly craf...   299.00  0.00    2010-07-30 13:58:35
        6   1   rodriguez   Rodriguez Caballero 11  Featuring a carefully chosen, solid Canadian cedar...   415.00  39.00   2010-07-30 14:12:41
        7   2   precision   Fender Precision    The Fender Precision bass guitar delivers the soun...   799.99  30.00   2010-06-01 11:29:35
        8   2   hofner  Hofner Icon With authentic details inspired by the original, t...   499.99  25.00   2010-07-30 14:18:33
        9   3   ludwig  Ludwig 5-piece Drum Set with Cymbals    This product includes a Ludwig 5-piece drum set an...   699.99  30.00   2010-07-30 12:46:40
        10  3   tama    Tama 5-Piece Drum Set with Cymbals  The Tama 5-piece Drum Set is the most affordable T...   799.99  15.00   2010-07-30 13:14:15

第一列是产品ID,第二列是类别ID。

Let me explain. I have a simple query which works when I tested it on phpmyadmin but it does not return the right answer in my web application.

This is the function that runs the query:

function GetProductsFromCategoryId($categoryId)
{
    global $db;
    $query = '
        SELECT *
        FROM `products`
        WHERE categoryID = 3';
    try {
        $statement = $db->prepare($query);
        //$statement->bindValue(':categoryId', $categoryId);
        $statement->execute();
        $result = $statement->fetch();
        $statement->closeCursor();
        return $result;
    } catch (PDOException $e) {
        $error_message = $e->getMessage();
        display_db_error($error_message);
    }
}

usually categoryID = $categoryID but I hard coded it with "3" because I wanted to see the results. There is whole other database file which handles the connection and I can post them if you want but the other functions work fine. This is a template that I use from Murach's php book ch 24. This function is my own, slightly modified.

With SELECT *, the function should return 2 arrays for for each product as you will see later when I post my result from phpmyadmin. Instead, it only return 1 array for one product.

This is the query I ran in phpmyadmin:

SELECT * FROM `products` WHERE categoryID=3;

and the result is:

        9   3   ludwig  Ludwig 5-piece Drum Set with Cymbals    This product includes a Ludwig 5-piece drum set an...   699.99  30.00   2010-07-30 12:46:40
        10  3   tama    Tama 5-Piece Drum Set with Cymbals  The Tama 5-piece Drum Set is the most affordable T...   799.99  15.00   2010-07-30 13:14:15

So, as you can see, it should return 2 array as that is what the fetch() function should do but it only returns one array. I have xdebug set up with netbeans and looking at the variable $result, it has 1 array with productName "Ludwig 5-piece..."

I have been stuck on this for a 3 days and I have zero idea why it does not work. Please please help!

Thanks!!!

PS: Here is the products on table on the database

        1   1   strat   Fender Stratocaster The Fender Stratocaster is the electric guitar des...   699.00  30.00   2009-10-30 09:32:40
        2   1   les_paul    Gibson Les Paul This Les Paul guitar offers a carved top and humbu...   1199.00 30.00   2009-12-05 16:33:13
        3   1   sg  Gibson SG   This Gibson SG electric guitar takes the best of t...   2517.00 52.00   2010-02-04 11:04:31
        4   1   fg700s  Yamaha FG700S   The Yamaha FG700S solid top acoustic guitar has th...   489.99  38.00   2010-06-01 11:12:59
        5   1   washburn    Washburn D10S   The Washburn D10S acoustic guitar is superbly craf...   299.00  0.00    2010-07-30 13:58:35
        6   1   rodriguez   Rodriguez Caballero 11  Featuring a carefully chosen, solid Canadian cedar...   415.00  39.00   2010-07-30 14:12:41
        7   2   precision   Fender Precision    The Fender Precision bass guitar delivers the soun...   799.99  30.00   2010-06-01 11:29:35
        8   2   hofner  Hofner Icon With authentic details inspired by the original, t...   499.99  25.00   2010-07-30 14:18:33
        9   3   ludwig  Ludwig 5-piece Drum Set with Cymbals    This product includes a Ludwig 5-piece drum set an...   699.99  30.00   2010-07-30 12:46:40
        10  3   tama    Tama 5-Piece Drum Set with Cymbals  The Tama 5-piece Drum Set is the most affordable T...   799.99  15.00   2010-07-30 13:14:15

The first column is the productID and the second column is the categoryID.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

初吻给了烟 2024-10-24 11:42:41

您的 $statement->fetch() 仅从数据库返回一行。请参阅 PDOStatement::fetch()。我认为您想要 PDOStatement::fetchAll() 而返回查询中的所有行。

Your $statement->fetch() only returns a single row from the database. See PDOStatement::fetch(). I think you want PDOStatement::fetchAll() instead to return all rows from your query.

葬シ愛 2024-10-24 11:42:41

PDOStatement::fetch() 一次仅返回一行。您必须迭代调用它才能获取剩余的行。

PDOStatement::fetch() only returns one row at a time. You must call it iteratively to get the remaining rows.

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