Forum Moderators: coopster

Message Too Old, No Replies

Simple MySQL query using PHP

Getting the corresponding category name for an id

         

encyclo

11:43 pm on Jan 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi, I'm incredibly new to all this PHP + MySQL stuff - I'm using a pre-written directory script that I want to personalise a bit. There is a table called categories, and in that table, there are fields for "id" and "cat_name". The URL used is in the form index.php?cat_id=1

What I need to do is to display the corresponding cat_name for the id in the URL. Is this easy? Where do I start? How do I get to learn all this stuff?!

Thanks for your help for such a newbie question!

webwonderuk

12:13 am on Jan 6, 2004 (gmt 0)

10+ Year Member



<?php
if (!($connection = @ mysql_connect(localhost, User, Password)))
die("could not connect");
if (!(mysql_select_db(database, $connection)))
showerror();
$result = mysql_query("SELECT cat_name from categories WHERE id = '$cat_id'");
$myrow = mysql_fetch_row($result);
echo $myrow[0];
?>

Should do the trick.
You are basically firdt of all connecting to the database and then running a query which is to select the category name from the database categories when the id matches the cat_id sent by the server.
$myrow then fetches the result from that row and the Echo returns the result to output to the browser.
Havent tested it though.:-)

coopster

2:40 pm on Jan 6, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



encyclo, you may want to check the article in the PHP Forum Library [webmasterworld.com] titled Basics of extracting data from MySQL using PHP [webmasterworld.com]. Although webwonderuk has pushed you in the right direction I think you'll find the thread quite useful. And Welcome to the WebmasterWorld PHP Forum :)

encyclo

6:56 pm on Jan 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks to webwonderuk and coopster - it works! I had to adapt the example a bit, but the library post led me in the right direction. I'm now going to read and learn as much as I can from this forum, so I don't need to ask such easy questions!