发布数据库查询的返回值

时间:2012-06-01 15:32:24

标签: php mysql database mysqli

我想弄清楚如何获得要发布的返回值。我的代码如下

    <?php

$mysqli = new mysqli("localhost","user","password","db");

$lname=$_POST['lname'];


$query= "SELECT * FROM contacts WHERE lname = '".$lname."'";
$result = $mysqli->query($query);     
if (!$result) {
  printf("Query failed: %s\n", $mysqli->error);
  exit;
}      

return $result;
?>

我对php很新,但我正在做的是将一个名字发布到php文件,然后转到数据库,找到所有输入姓氏的值,然后我希望它返回值然后打印出来

1 个答案:

答案 0 :(得分:0)

请参阅mysqli_result::fetch_assoc

<?php

$mysqli = new mysqli("localhost","user","password","db");

$lname=$_POST['lname'];


$query= "SELECT * FROM contacts WHERE lname = '".$lname."'";
$result = $mysqli->query($query);     
if (!$result) {
  printf("Query failed: %s\n", $mysqli->error);
  exit;
}      
while ($contact = $result->fetch_assoc()) {
    echo $contact['name']."<br>\n";
}
?>