php – 如何在mysqli中运行select查询

我需要显示特定的记录,记录存在于db但它什么也没显示.我的代码是.

 $con=mysqli_connect("localhost","root","","test") or die("Connecting to MySQL failed"); 


$name=$_POST['uname'];

$query = "SELECT * FROM officedb WHERE name='.$name.'";
$data=mysqli_query($con,$query);   

while($row=mysqli_fetch_array($data)){
    echo $row['name'];
    echo $row['lname'];
    echo $row['department'];
}

最佳答案 你是$name变量的错误,只需更改:

$query = "SELECT * FROM officedb WHERE name='.$name.'";

有了这个:

$query = "SELECT * FROM officedb WHERE name = '$name'";

您的查询如下:

SELECT * FROM officedb WHERE name = '.test.'

更改后,我建议查询应该是:

SELECT * FROM officedb WHERE name = 'test'
点赞