Getting specifies info from MYSQL

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
Zeceer
Forum Contributor
Posts: 136
Joined: Fri Aug 02, 2002 5:10 am
Location: Norway

Getting specifies info from MYSQL

Post 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
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
Zeceer
Forum Contributor
Posts: 136
Joined: Fri Aug 02, 2002 5:10 am
Location: Norway

Post 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
User avatar
releasedj
Forum Contributor
Posts: 105
Joined: Tue Jun 17, 2003 6:35 am

Post 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.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
Post Reply