Page 1 of 1

Getting specifies info from MYSQL

Posted: Fri Jun 20, 2003 6:09 am
by Zeceer
Hi,

I have a table with four colums. the first column is the name of the category that the product are in. (this is a webshop). What I want is to output only the products that are in the one category. If this is the database:

shoes : brown shoes : 100 dollars : great shoes
sunglasses : brown shoes : 100 dollars : great shoes
shoes : brown shoes : 100 dollars : great shoes
sunglasses : brown shoes : 100 dollars : great shoes

Then I only want to list the products that have shoes as category. I could need some info about which mysql function to use. Are kind of new when it comes to using mysql :D

Posted: Fri Jun 20, 2003 7:19 am
by twigletmac
You'd want to add a WHERE clause to the SELECT statement:
http://www.mysql.com/doc/en/SELECT.html
(I'm only guessing at your table and field names)

Code: Select all

SELECT category, item, price, description FROM products WHERE category = 'shoes'
Mac

Posted: Fri Jun 20, 2003 8:04 am
by Zeceer
Thats it, thanx! Just one thing, I'm trying to learn my self the syntax of MYSQL and have been trying to get this working:

$result = mysql_query( "SELECT * FROM products WHERE category=".$_POST['id']."" );

The "id" variable comes from a post like this test.php?id=shoes

I've tried some combinations but haven't made it work

Posted: Fri Jun 20, 2003 8:12 am
by releasedj
Try:

Code: Select all

$result = mysql_query( "SELECT * FROM products WHERE category='".$_POST['id']."'" );
Single quotes around the ID. this is if the id is a string.

If that doesn't work, then echo the final string and post it to the forum, maybe we can help.

Posted: Fri Jun 20, 2003 8:17 am
by twigletmac
It's always useful to add some error handling to your database functions, so instead of simply:

Code: Select all

$result = mysql_query( "SELECT * FROM products WHERE category='".$_POST['id']."'" );
you would be able to debug much easier if you had:

Code: Select all

$sql = "SELECT * FROM products WHERE category='".$_POST['id']."'";
$result = mysql_query($sql) or die(mysql_error().'<p>'.$sql.'</p>');
Mac