dongna9185 2018-05-12 15:54
浏览 59
已采纳

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!

  • 写回答

1条回答 默认 最新

  • douyuan4825 2018-05-12 16:04
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据