获取单个数据会返回错误

时间:2015-12-02 09:25:33

标签: php android sql database mysqli

我正在尝试在我的服务器数据库中获取一些单个数据,但这会引发一些错误。传入的数据是正确的。搜索功能无法完成。

以下是代码:

<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
define('HOST','xxxxxxxxxxx');
define('USER','xxxxxxxxxxxx');
define('PASS','xxxxxxxxx');
define('DB','xxxxxxxxxx');
 $con = mysqli_connect(HOST,USER,PASS,DB);

 $post_id = $_POST['id'];
 $buyer_mobile = $_POST['mobile'];
 $buyer_name = $_POST['name'];

$sql = "select mobile from flatowner where id='$post_id'";
$res = mysqli_query($con,$sql);
$owner_mobile = $row['mobile'];



$sql = "select name from user where mobile='$owner_mobile'";
$r = mysqli_query($con,$sql);
$owner_name = $row['name'];
$sql = "INSERT INTO flat_booking         (post_id,owner_mobile,owner_name,buyer_mobile,buyer_name) VALUES         ('$post_id','$owner_mobile','$owner_name','$buyer_mobile','$buyer_name')";
if(mysqli_query($con,$sql)){
 echo "Success";
 }
 else{
 echo "error";
 }
mysqli_close($con);
}else{
echo 'error1';
}    

我在这里做错了什么?也许这个:

  

$ owner_mobile = $ row ['mobile'];

提前致谢!

2 个答案:

答案 0 :(得分:1)

{{1}}

答案 1 :(得分:0)

你的问题就在这一行:

$owner_mobile = $row['mobile'];

您尚未创建$row变量。为此,您需要执行以下操作:

首先执行此操作:

<?php
$row = array();
while ($result = mysqli_fetch_assoc($res))
{
    $row[] = $result;
}
?>

这允许您这样做:

<?php
foreach ($row as $r)
{
    var_dump($r); print "<br />"; // One row from the DB per var dump
}
?>