[SOLVED] MYSQl query

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
erinther
Forum Newbie
Posts: 19
Joined: Sun Dec 11, 2005 2:07 pm

[SOLVED] MYSQl query

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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 ;)
erinther
Forum Newbie
Posts: 19
Joined: Sun Dec 11, 2005 2:07 pm

Post 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?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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'];
erinther
Forum Newbie
Posts: 19
Joined: Sun Dec 11, 2005 2:07 pm

Post 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?
erinther
Forum Newbie
Posts: 19
Joined: Sun Dec 11, 2005 2:07 pm

Post by erinther »

You know what? I did it. There were only minor errors in syntax. Thank you very much.I am really grateful.
Post Reply