Jump to content

Simple problem


idgeit

Recommended Posts

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.