Jump to content

[SOLVED] drawing data from the sql database with mutliple rows in php


Recommended Posts

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.

 

$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++;
}

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.

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++;
}

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.