My correct SQL query (tested on phpmyadmin) is returning wrong result in PHP
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 i开发者_JAVA百科s 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.
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.
PDOStatement::fetch() only returns one row at a time. You must call it iteratively to get the remaining rows.
精彩评论