Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

returing one value from the database using php

Tags:

php

mysql

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"
like image 974
djd Avatar asked Oct 05 '10 18:10

djd


People also ask

How do you get a single value from a database?

The Command object provides the capability to return single values using the ExecuteScalar method. The ExecuteScalar method returns, as a scalar value, the value of the first column of the first row of the result set. The following code example inserts a new value in the database using a SqlCommand.

How can I get single data from MySQL in PHP?

Data can be fetched from MySQL tables by executing SQL SELECT statement through PHP function mysql_query. You have several options to fetch data from MySQL. The most frequently used option is to use function mysql_fetch_array(). This function returns row as an associative array, a numeric array, or both.

How can I get single record database in PHP?

Displaying all details of single record php"; $count="SELECT * FROM student where id=?"; if($stmt = $connection->prepare($count)){ $stmt->bind_param('i',$id); $stmt->execute(); $result = $stmt->get_result(); echo "No of records : ".


2 Answers

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

like image 82
KoolKabin Avatar answered Sep 28 '22 07:09

KoolKabin


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"];
?>
like image 26
stevendesu Avatar answered Sep 28 '22 06:09

stevendesu