idgeit Posted February 22, 2007 Share Posted February 22, 2007 Hey all, Im fairly new to Mysql and PHP. I Managed to connect to a database and get it to echo the result, only problem is that it prints the whole row, can anyone tell me how i can just print out a single part of the row? Heres what i got, <?php $handle = mysql_connect('host', 'user', 'password'); if($handle == false){ die("Could not connect to MySQL. Exiting.\r\n"); } $db = mysql_select_db('dbname'); if($db == false){ die("Could not select MySQL DB. Exiting.\r\n"); } $query = 'SELECT store.product_name, store.product_num FROM store'; $result = mysql_query($query, $handle); while($row = mysql_fetch_assoc($result)){ $product_name = $row["product_name"]; $product_num = $row["product_num"]; echo ("$product_name"); echo ("$product_num"); } mysql_close($handle); ?> Thanks! ~ Idgeit Link to comment https://forums.phpfreaks.com/topic/39714-simple-problem/ Share on other sites More sharing options...
jcbarr Posted February 22, 2007 Share Posted February 22, 2007 What does this actually display? Link to comment https://forums.phpfreaks.com/topic/39714-simple-problem/#findComment-191715 Share on other sites More sharing options...
idgeit Posted February 22, 2007 Author Share Posted February 22, 2007 A list of everything in product_name and product_num Link to comment https://forums.phpfreaks.com/topic/39714-simple-problem/#findComment-191727 Share on other sites More sharing options...
jcbarr Posted February 22, 2007 Share Posted February 22, 2007 That is what you have told it to do. This part right here while($row = mysql_fetch_assoc($result)){ $product_name = $row["product_name"]; $product_num = $row["product_num"]; echo ("$product_name"); echo ("$product_num"); } Tells it to loop through the entire result and echo the values for each row until there are no more rows to show. If you just want to show one row, you can limit your query to only one row, or search for a row that meets a certain criteria. An easy way to do this is to edit your query. Make it look something like this. $query = "SELECT store.product_name, store.product_num FROM store WHERE column_name='somevalue'"; or... $query = 'SELECT store.product_name, store.product_num FROM store LIMIT 1'; Link to comment https://forums.phpfreaks.com/topic/39714-simple-problem/#findComment-191734 Share on other sites More sharing options...
idgeit Posted February 23, 2007 Author Share Posted February 23, 2007 It worked! Thanks for all the help, ~ Idgeit Link to comment https://forums.phpfreaks.com/topic/39714-simple-problem/#findComment-192200 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.