Jump to content

[SOLVED] I need help getting results from my DB


m7med

Recommended Posts

I have a table that contains orders for different products. the table has (id,username,product,date..etc) fields

 

so I want to get the usernames that ordered a certain product.

 

so I used this code:

 

$query  = "SELECT username FROM orders WHERE product='Pens' ";
$result = mysql_query($query);

while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
    echo "Username :{$row['name']} <br>";
} 

 

so I get the usernames that ordered pens but if for example John ordered pens 4 times I would get his username printed 4 times.

 

so I want the username to be printed only once.

 

any suggestions ?

modify your query

From

$query  = "SELECT username FROM orders WHERE product='Pens' ";

 

To

$query  = "SELECT distinct username FROM orders WHERE product='Pens' ";

 

It will pick distinct username who have buyed product pen from your orders table.

 

Regards

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.