trampolinejoe Posted November 25, 2008 Share Posted November 25, 2008 Hello all you very helpful people, if you guys have been online for the last few days you may have noticed i have been trying to build a shopping cart. I have a table in my database that stores all the products that are ordered. Also, it gives each product a order to belong to. basically: A customer HAS A Order and a order HAS A product I am trying to withdraw the data from the database but the thing is a customer can have multiple products. My code only works if the customer has a single product. My query brings back multiple rows as the customer has multiple products ordered. Ill show you what I have: $order = mysql_query("SELECT * FROM OrderProducts WHERE OrderId =" . $connote ); while($row = mysql_fetch_array($order)) { $product = $row['ProductId']; } Iam sure there is away in php to add each product to some kind of product array. I tried afew ideas but it got errors since I don't know the correct syntax, but i am guessing i need some kind of loop that appends the product to the array for each product that there is. No idea how to actually do it. I am sure somebody here can teach me how to do it. Cheers, Joe. Quote Link to comment https://forums.phpfreaks.com/topic/134189-solved-drawing-data-from-the-sql-database-with-mutliple-rows-in-php/ Share on other sites More sharing options...
waynew Posted November 25, 2008 Share Posted November 25, 2008 $order = mysql_query("SELECT * FROM OrderProducts WHERE OrderId =" . $connote ); while($row = mysql_fetch_array($order)) { $product = $row['ProductId']; //overwriting $product each time!!!!!!!! } I am guessing that this is because you are overwriting $product each time: Try $order = mysql_query("SELECT * FROM OrderProducts WHERE OrderId =" . $connote ); $products = array(); while($row = mysql_fetch_array($order)) { array_push($products,$row['ProductId']); //place product ids in array } //output array $i = 0; while($i < sizeof($products)){ echo $products[$i]; $i++; } Quote Link to comment https://forums.phpfreaks.com/topic/134189-solved-drawing-data-from-the-sql-database-with-mutliple-rows-in-php/#findComment-698510 Share on other sites More sharing options...
trampolinejoe Posted November 25, 2008 Author Share Posted November 25, 2008 hello waynewex dosnt seem to be working, getting the same error actually Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource for this line while($row = mysql_fetch_array($order)) I see myself overwriting product, so i see what you had done to add to the array using array_push. But yeah, if i run the query directly in the DB it works perfectly fine. Cheers, Joe. Quote Link to comment https://forums.phpfreaks.com/topic/134189-solved-drawing-data-from-the-sql-database-with-mutliple-rows-in-php/#findComment-698519 Share on other sites More sharing options...
waynew Posted November 25, 2008 Share Posted November 25, 2008 It's happening because your query is failing somehow. Could you use this and tell me what it outputs? $order = mysql_query("SELECT * FROM OrderProducts WHERE OrderId =" . $connote ); echo "<h2>SELECT * FROM OrderProducts WHERE OrderId =" . $connote."</h2>"; $products = array(); while($row = mysql_fetch_array($order)) { array_push($products,$row['ProductId']); //place product ids in array } //output array $i = 0; while($i < sizeof($products)){ echo $products[$i]; $i++; } Quote Link to comment https://forums.phpfreaks.com/topic/134189-solved-drawing-data-from-the-sql-database-with-mutliple-rows-in-php/#findComment-698524 Share on other sites More sharing options...
Alexhoward Posted November 25, 2008 Share Posted November 25, 2008 I'd use sessions to hold the info, then put it in to SQL when they continue to the checkout try using: foreach http://uk3.php.net/foreach Quote Link to comment https://forums.phpfreaks.com/topic/134189-solved-drawing-data-from-the-sql-database-with-mutliple-rows-in-php/#findComment-698526 Share on other sites More sharing options...
trampolinejoe Posted November 25, 2008 Author Share Posted November 25, 2008 waynewex, I found the problem, I needed the ' around $connote. It works now, thanks alot $order = mysql_query("SELECT * FROM OrderProducts WHERE OrderId =" . "'$connote'" ); instead of $order = mysql_query("SELECT * FROM OrderProducts WHERE OrderId =" . $connote ); Thank you so much for helping be solve my problem, next time i will know what to do. Cheers, Joe. Quote Link to comment https://forums.phpfreaks.com/topic/134189-solved-drawing-data-from-the-sql-database-with-mutliple-rows-in-php/#findComment-698532 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.