0

Hello i'm trying to check if phoneNumber exist in data base and then return a response but i'm getting array to sting convertion warning

Javascript code :

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 code :

<?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);
?>

and here what i'm getting as response now it's seems that i'm not accessing the phoneNumber in the array :

<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}]
Main Flow
  • 319
  • 2
  • 18
  • 1
    [Little Bobby](http://bobby-tables.com/) says ***[your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)*** Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php). Even [escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) is not safe! – Jay Blanchard Apr 13 '17 at 12:49
  • 1
    Might be useful to know where _And see the complete error message_ – RiggsFolly Apr 13 '17 at 12:50
  • What's the warning/error you got? – borracciaBlu Apr 13 '17 at 12:50
  • You are trying to print an Array.. You should print an specific position of this array.. Where exactly is this error? – Xidh Apr 13 '17 at 12:51
  • the error is "Array to string conversion in " in mysqli_query – Main Flow Apr 13 '17 at 12:51
  • Start by changing `$S[]=$_POST['data'];` to `$S = $_POST['data'];` – RiggsFolly Apr 13 '17 at 12:52
  • $_POST is already an array and you try to use it with $s[]; ? – OldPadawan Apr 13 '17 at 12:52
  • Do a `file_put_contents('debug.txt', print_r($_POST['data'],1));` and then look at what you are actually passing in that `$_POST` variable by editing the `debug.txt` file – RiggsFolly Apr 13 '17 at 12:56
  • 1
    ah yes true RiggsFolly and OldPadawan i was wrong but i think i'm not checking the phoneNumber in the query tel='$S[$i]' it should be something like this tel='$S[$i]->phoneNumbers[0]' ? – Main Flow Apr 13 '17 at 12:58
  • i added the php response in the question – Main Flow Apr 13 '17 at 13:25
  • here the debug.txt response Array ( [0] => Array ( [displayName] => Nozha [phoneNumbers] => Array ( [0] => 97925955 ) ) [1] => Array ([displayName] => Maher [phoneNumbers] => Array ([0] => 97925955))) – Main Flow Apr 13 '17 at 13:34

1 Answers1

-1

Can you try this,

<?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);

?>
Burak Keskin
  • 125
  • 3
  • 6
  • Do or do not. There is no "try". A ***good answer*** will always have an explanation of what was done and why it was done in such a manner, not only for the OP but for future visitors to SO. – Jay Blanchard Apr 13 '17 at 13:05
  • 2
    @JayBlanchard : `Do or do not. There is no "try"` this quote has been approved by all padawan ! But `when 900 years old, you reach…`(sorry for this unrelevant comment, but cap'tain obvious could not miss this one... ^^) EDIT: I'll delete if it bothers people, sorry – OldPadawan Apr 13 '17 at 13:14