Php:mysqli错误:警告:array_push()期望参数1为数组,在[关闭]中给出null

Php:mysqli错误:警告:array_push()期望参数1为数组,在[关闭]中给出null

问题描述:

I created this PHP code to do a search in a database with data in person, but every time I run it gives me this error and I do not understand why.

<?php
$host='localhost';
$username='user';
$pwd='password';
$db='PersonDB';
$responce = array();
$responce ["success"]= 0 ;
$responce ["message"]= "Error at start";
global $found;

if (isset($_POST['idperson']) && isset($_POST['name']) && isset($_POST['surname'])){
    $idperson = $_POST['idperson'];
    $name = $_POST['name'];
    $surname = $_POST['surname'];
    $connect = mysqli_connect($host,$username,$pwd,$db) or die ('Unabletp connect');
    if (mysqli_connect_error($connect)){
        echo(" Fallita la connessione al database".mysqli_connect_error());
    }
    $sql_find = ("SELECT CITY, ADRESS, CAP FROM PEOPLE WHERE ID_PERSON = $idperson'  AND NAME_PERSON = '$name'  AND SURNAME = '$surname' ") ;
    print $sql_find;
    $result= mysqli_query($connect,$sql_find) or  die ("Error:".mysqli_error($connect)." with query ");
    if($result){
        $found=true;
        while ($row = mysql_fetch_array($result)){
            array_push($response, array("CITY"=>$row["CITY"],"ADRESS" = >$row["ADRESS"],"CAP" => $row["CAP"],"FOUND" => $found));
        }
        echo json_encode($response);
     } else {
        $responce ["success"]= 0 ;
        $responce ["message"]= "Person not found";
} else {
    $response['success']=0;
    $response['message']="Parameters are not correct";
    echo json_encode($response);
}
}
mysqli_close($connect);
?>

Printing the query the data is passed correctly but I do not understand why! do not need array_push() to send my json file containing the data found to my app ?

Thanks for your help!

Check your $response variable's data before this while loop, you will understand why it's throwing this error message. The message is very obvious, it's not finding any data for $response variable. you have declared $responce as global variable, I think, it should be $response at that place.