0

I've searched this here and on the internet but can't find a solution.

I'm posting a JSON array like :

[{"phone_number":"+12345678"},
 {"phone_number":"+23456789"},
 {"phone_number":"34567890"},
 {"phone_number":"45678901"} 
 etc... etc...

Here's my code :

<?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
require('dbConnect.php');

//post all contacts in my phone as a JSON array
$json = $_POST['phonenumber'];
$array = json_decode($json);

    foreach ($array as $value)
    {
        $phonenumber = $value->phone_number;

$stmt = $con->prepare('SELECT * FROM user WHERE username = ?');
$stmt->bind_param('s', $phonenumber);
$stmt->execute(); 
echo $phonenumber . "<br>";
var_dump($stmt);
}
?>

Only the first phone number in the array echos properly. Then I get :

object(mysqli_stmt)#131 (10) { ["affected_rows"]=> int(-1) ["insert_id"]=> int(0) ["num_rows"]=> int(0) ["param_count"]=> int(1) ["field_count"]=> int(2) ["errno"]=> int(0) ["error"]=> string(0) "" ["error_list"]=> array(0) { } ["sqlstate"]=> string(5) "00000" ["id"]=> int(1) } 
Fatal error: Call to a member function bind_param() on a non-object in /var/www/html/checkcontact.php on line 28

Line 28 is : $stmt->bind_param('s', $phonenumber);

When I use this code (not safe though) it works fine, all the phone numbers get echoed correctly :

<?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
require('dbConnect.php');

//post all contacts in my phone as a JSON array
$json = $_POST['phonenumber'];
$array = json_decode($json);

    foreach ($array as $value)
    {
        $phonenumber = $value->phone_number;

                $sql = "SELECT * FROM user WHERE username = '$phonenumber'";
        $result = mysqli_query($con, $sql);


echo $phonenumber . "<br>";

    } 
        ?>
CHarris
  • 2,693
  • 8
  • 45
  • 71
  • You're not checking for the real error. – Funk Forty Niner Apr 29 '17 at 15:39
  • It's telling you that `$stmt` is not an object -- so what is it? – alanlittle Apr 29 '17 at 16:03
  • @Fred-ii- So even though I have display_errors and display_startup_errors set at the top of my file it decides not to show me what sort of error I might be having. Fantastic. – CHarris Apr 29 '17 at 16:20
  • Not just that, but `mysqli_error($con)` to the query. – Funk Forty Niner Apr 29 '17 at 17:43
  • btw; did you close off the brace for `foreach ($array as $value){` ? – Funk Forty Niner Apr 29 '17 at 17:48
  • @Fred-ii- Yes, I have that bracket in my code, sorry it wasn;t in the question - since edited. I'll look into mysqli_error($con) shortly. I have display_errors = on set in php.ini, everything I can think of, just wish I could get descriptions of errors in Php without so much trouble. – CHarris Apr 29 '17 at 18:00
  • I added 'or die(mysqli_error($con));' after my SQL statement, the Fatal error is gone and now I have no errors but still not convinced everything is working properly. When I echo $phonenumber still only the first one shows up, not the whole JSON array. – CHarris Apr 30 '17 at 09:05

0 Answers0