tags:

views:

39

answers:

3

How do I fetch only one value from a database using PHP?
I tried searching almost everywhere but don't seem to find solution for these e.g., for what I am trying to do is

"SELECT name FROM TABLE
WHERE UNIQUE_ID=Some unique ID"
+1  A: 

how about following php code:

$strSQL = "SELECT name FROM TABLE WHERE UNIQUE_ID=Some unique ID";
$result = mysql_query($strSQL) or die('SQL Error :: '.mysql_error());
$row = mysql_fetch_assoc($result);
echo $row['name'];

I hope it give ur desired name.

Steps:
1.) Prepare SQL Statement.
2.) Query db and store the resultset in a variable
3.) fetch the first row of resultset in next variable
4.) print the desire column

KoolKabin
+1  A: 

Here's the basic idea from start to finish:

<?php
$db = mysql_connect("mysql.mysite.com", "username", "password");
mysql_select_db("database", $db);
$result = mysql_query("SELECT name FROM TABLE WHERE UNIQUE_ID=Some unique ID");
$data = mysql_fetch_row($result);

echo $data["name"];
?>
steven_desu
A: 

You can fetch one value from the table using this query :

"SELECT name FROM TABLE WHERE UNIQUE_ID=Some unique ID limit 1"

Notice the use of limit 1 in the query. I hope it helps!!

Guru Charan