更新数据库表并使用mysqli_fetch获取适当的行时出错

时间:2018-04-26 05:43:36

标签: php mysql mysqli

我正在尝试向此php脚本发送登录信息,并在用户名和密码存在时更新数据库中的某些字段。但是我收到了mysqli_fetch_array的错误。这里有很多类似的问题,我知道问题是“$ result”(当我打印出结果的值为1时),可能是因为查询失败!但是当我在mysql中执行只是查询时,它会毫无问题地更新字段,所以我真的很无能。非常感谢任何帮助! 谢谢。

这是php代码:

<?php
if(isset($_POST) == true && empty($_POST) == false){
    $latitude = $_POST['Latitude'];
    $longitude = $_POST['Longitude'];
    $timestamp = $_POST['Timestamp'];
        $Password=$_POST['Password'];   
        $name=$_POST['userName'];


    $dbname = "android_db";
    $servername = "localhost";
    $username = "root";
    $password = "1234";

    //connect to the database
    $conn = mysqli_connect($servername, $username, $password, $dbname);
    if(!$conn)
    {
        die("Connection Failed" . mysqli_error($conn));
    }
$sql = "Update LocationTable SET Latitude='$latitude', Longitude='$longitude' WHERE name='$name' AND Password='$Password';";

$result = mysqli_query($conn, $sql);
echo"$result";

$response = array();

while($row = mysqli_fetch_array($result)){
    $response = array("id"=>$row[0],"name"=>$row[1],"password"=>$row[2],"email"=>$row[3]);
}

echo json_encode(array("user_data"=>$response));
 }
?>

这是输出:

1
<br />
<b>Warning</b>:  mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in
<b>/opt/lampp/htdocs/friendFinder/login.php</b> on line
<b>28</b>
<br />
{"user_data":[]}

这些是数据库表中的字段 enter image description here

1 个答案:

答案 0 :(得分:3)

<?php
if(isset($_POST) == true && empty($_POST) == false){
    $latitude = $_POST['Latitude'];
    $longitude = $_POST['Longitude'];
    $timestamp = $_POST['Timestamp'];
        $Password=$_POST['Password'];   
        $name=$_POST['userName'];


    $dbname = "android_db";
    $servername = "localhost";
    $username = "root";
    $password = "1234";

    //connect to the database
    $conn = mysqli_connect($servername, $username, $password, $dbname);
    if(!$conn)
    {
        die("Connection Failed" . mysqli_error($conn));
    }
$sql = "Update LocationTable SET Latitude='$latitude', Longitude='$longitude' WHERE name='$name' AND Password='$Password';";

$result1 = mysqli_query($conn, $sql); // update action

$result = mysqli_query($conn, "select *from LocationTable"); // fetch action


$response = array();

while($row = mysqli_fetch_array($result)){
    $response = array("id"=>$row[0],"name"=>$row[1],"password"=>$row[2],"email"=>$row[3]);
}

echo json_encode(array("user_data"=>$response));
 }
?>