致命错误:在非对象JSON数组上调用成员函数bind_param()

时间:2017-04-29 15:27:48

标签: php mysql bindparam

我在这里和互联网上搜索了这个但是找不到解决方案。

我发布了一个JSON数组,如:

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

这是我的代码:

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

只有阵列中的第一个电话号码正确回声。然后我得到:

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

第28行是:$stmt->bind_param('s', $phonenumber);

当我使用此代码(虽然不安全)时,它工作正常,所有电话号码都能正确回显:

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

    } 
        ?>

0 个答案:

没有答案