SQL查询很好,但以下代码不返回任何值。我需要显示查询中的单个值。
$clientId=$row["clientid"];
$sql2="select cname from Clients where clientid='$clientId'";
//$result = $conn->query($sql2);
$result = mysql_query($sql2);
echo $result;
答案 0 :(得分:0)
为了使用你需要从中获取的结果。 E.g:
$result = mysql_query($sql2);
$row = mysql_fetch_array($result);
$cname = $row[0];
答案 1 :(得分: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;
}
答案 2 :(得分:0)
尝试这种方式并使用(`)标记来修复您的SQL查询错误,但首先请参阅此弃用API的文档,而不是使用 PDO 或 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;
N.B:使用PDO或MYSQLi,用于修复您的SQL INJECTION
相关问题的预备语句
答案 3 :(得分:0)
这是你应该如何使用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)
}
答案 4 :(得分:0)
不推荐使用mysql
你正在应用错误的方法。
您的代码应如下所示:
<?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);
?>
还准备好更多&gt;&gt; http://www.w3schools.com/php/func_mysqli_fetch_array.asp