The SQL query is good but the following code doesn't return any value. I need to display just single value from the query.
$clientId=$row["clientid"];
$sql2="select cname from Clients where clientid='$clientId'";
//$result = $conn->query($sql2);
$result = mysql_query($sql2);
echo $result;
In order to use the result you need to fetch from it. Eg:
$result = mysql_query($sql2);
$row = mysql_fetch_array($result);
$cname = $row[0];
$clientId=$row["clientid"];
$sql2="select cname from Clients where clientid='$clientId'";
//$result = $conn->query($sql2);
$result = mysql_query($sql2);
foreach($result->result() as $row){
echo $row->cname;
}
Try this way and fix the typo on your sql query use (`) mark for column names, but first see documentation for this deprecated API, instead use PDO or MYSQLi .see mysql_query
$clientId=$row["clientid"];
$sql2="select `cname` from Clients where clientid='$clientId'";
$result = mysql_query($sql2);
$row = mysql_fetch_assoc($result);
//use column name,as you fetch result as associative array
$canme = $row['cname'];
echo $canme;
NB: Use PDO or MYSQLi, for prepared statement that will fix your SQL INJECTION
related Problems
This is how you should do it using mysqli
$query = 'SELECT cname FROM Clients WHERE clientid = ?';
if($stmt = $mysqli->prepare($query)){
$stmt->bind_param('i', $_POST['clientid']);
$stmt->execute();
$stmt->bind_result($cname);
$stmt->fetch();
echo $cname; // (optional, prints it out)
}
mysql deprecated
You are applying wrong methods.
Your code should look like :
<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="SELECT Lastname,Age FROM Persons ORDER BY Lastname";
$result=mysqli_query($con,$sql)
// Numeric array
$row=mysqli_fetch_array($result,MYSQLI_NUM);
printf ("%s (%s)\n",$row[0],$row[1]);
// Associative array
$row=mysqli_fetch_array($result,MYSQLI_ASSOC);
printf ("%s (%s)\n",$row["Lastname"],$row["Age"]);
// Free result set
mysqli_free_result($result);
mysqli_close($con);
?>
Also ready more >> http://www.w3schools.com/php/func_mysqli_fetch_array.asp
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.