PHP数组到循环内的字符串转换警告

时间:2017-04-13 12:49:13

标签: javascript php jquery ajax

您好我试图检查数据库中是否存在phoneNumber然后返回响应但是我正在获取sting转换警告

Javascript代码:

var c = [
    {
        "displayName" : "Nozha",
        "phoneNumbers": ["97925955"]
    },
    {
        "displayName": "Maher",
        "phoneNumbers": ["97925955"]
    }]
    checkUser(c)


    function checkUser(data){
        //hne 3ayet lel service php mte3ek w na7i return false
        $.ajax({
        url : "https://nozha.000webhostapp.com/verifecontact.php",
        type : "POST", 
        data  : {"data":data},             
           success:function(data) {                                         
                console.log(data);
            }
        });
    }

PHP代码:

<?php
 $S = $_POST['data'];
 for($i=0; $i<COUNT($S);$i++){
  $result=mysqli_query($con,"SELECT * from  user where tel='$S[$i]['phoneNumbers']'");
  if(mysqli_num_rows($result)>0){
    $S[$i]['success'] = true;
  }else{
   $S[$i]['success'] = false;
  }
}
echo json_encode($S);
?>

在这里,我现在得到的回应是,我似乎没有访问数组中的phoneNumber:

<br />
<b>Notice</b>:  Array to string conversion in 
<b>/storage/h2/007/664007/public_html/verifecontact.php</b> on line <b>9</b>
<br />
<br />
<b>Warning</b>:  mysqli_num_rows() expects parameter 1 to be mysqli_result, 
boolean given in <b>/storage/h2/007/664007/public_html/verifecontact.php</b> on line <b>10</b><br />
<br />
<b>Notice</b>:  Array to string conversion in 
<b>/storage/h2/007/664007/public_html/verifecontact.php</b> on line <b>9</b>
<br />
<br />
<b>Warning</b>:  mysqli_num_rows() expects parameter 1 to be mysqli_result, 
boolean given in <b>/storage/h2/007/664007/public_html/verifecontact.php</b> 
on line <b>10</b><br />
[{"displayName":"Nozha","phoneNumbers":["97925955"],"success":false},
{"displayName":"Maher","phoneNumbers":["97925955"],"success":false}]

1 个答案:

答案 0 :(得分:-1)

你能试试吗,

<?php
$result = '[
    {
        "displayName" : "Nozha",
        "phoneNumbers": ["97925955"]
    },
    {
        "displayName": "Maher",
        "phoneNumbers": ["97925955"]
    }]';

$result = json_decode($result, true);

for($i=0; $i < count($result); $i++){
    if(count($result) > 0){
        $result[$i]['success'] = true;
    }else{
        $result[$i]['success'] = false;
    }
}

$jsonResult = json_encode($result);

print_r($jsonResult);

?>