Page 1 of 1

[SOLVED] MYSQl query

Posted: Sun Dec 11, 2005 2:26 pm
by erinther
Hi,
I'm working on making a website and need something special that I think can be solved easily by php. I use MYSql and because I've no knwoledge of php and mysql, So I need your help:

In my database tables I've something like this:

Code: Select all

Category_id         category_lable 
196                         culture

Now I need a php code to put in my website sothat when someone inserts " culture" , then it checks my database and retrieve and prints its id (196).
I'm sure this is a very easy job for the most of you, but I've no idea how to do that. Thanks in advance for your help..
My php version is 4.4.1 and mysql is 4.1.13-standard.

Posted: Sun Dec 11, 2005 2:36 pm
by John Cartwright
look into mysql_connect() to establish a connection
look into mysql_select_db() to select the database
look into mysql_query() to perform the query
look into mysql_fetch_assoc() to fetch the result

Code: Select all

mysql_connect(..);
mysql_select_db(..);

//not sure how your user selects this, so I just hardcoded it
$id = 'culture';
$result = mysql_query('SELECT `category_id` WHERE `category_lable` = \''.$id.'\'') or die(mysql_error()); 

while ($result = mysql_fetch_assoc($result)) {
   echo $row['category_id']; 
}
that should get you started ;)

Posted: Sun Dec 11, 2005 3:10 pm
by erinther
Thanks a lot . You wrote:"//not sure how your user selects this, so I just hardcoded it". I want to use a form sothat users can insert the word.something like this:
<input name="category" type="text">

I write this here and hope you will combine this in php code. To be honest I'm afraid I may do some mistake doing that. So what will it be after adding a form and this input to the code?

Posted: Sun Dec 11, 2005 3:33 pm
by John Cartwright
if your form method is set to POST then you would change

Code: Select all

$id = $_POST['category'];
if the form method is set to GET then you would use

Code: Select all

$id = $_GET['category'];

Posted: Sun Dec 11, 2005 3:35 pm
by erinther
I got this error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE `category_lable` = 'culture' at line 1
What does it mean?

Posted: Sun Dec 11, 2005 4:31 pm
by erinther
You know what? I did it. There were only minor errors in syntax. Thank you very much.I am really grateful.