JSON响应无法用于php mysqli预处理语句

时间:2018-01-27 16:18:00

标签: php mysql json api mysqli

所以我不确定为什么我不能使用我在这个echo语句中使用的相同变量来为我准备好的语句将数据插入到mysql表中。它给了我一个值的布尔值。我确信我忽略了一些东西,但我似乎无法弄清楚是什么。

我应该将值存储在单独的数组中,然后将它们逐个拉出来吗?似乎多余......

错误:PHP致命错误:在第3764行的functions / functions.php中调用boolean上的成员函数bind_param()

不是建议的副本我在变量

上得到一个布尔值
global $con;
require "sendgrid-php/set_api.php";
$apiKey = getenv('SENDGRID_API_KEY');
$sg = new \SendGrid($apiKey);

$response = $sg->client->contactdb()->lists()->get();
$json_response = json_decode($response->body(), true);

foreach($json_response as $key){
    foreach($key as $type=>$value){

        $cat_id = $value['id'];
        $cat_name = $value['name'];
        $rec_count = $value['recipient_count'];
        echo "<option value='$cat_id'>$cat_name ($rec_count)</option>";

        $catid = $cat_id;
        $qry2 = $con->prepare("SELECT sg_id from email_campaign_categories where sg_id = ? ");
        $qry2->bind_param("s", $catid);
        $catid = $cat_id;
        $qry2->execute();
        $qry2->store_result();
        if($qry2->num_rows < 1){
            $ins_qry = $con->prepare("INSERT into email_campaign_categories(name,sg_id) VALUES(?,?)");
            $ins_qry->bind_param("ss", $catid, $cat_name);
            $ins_qry->execute();
        }
    }
}

工作代码:

global $con;
require "sendgrid-php/set_api.php";
$apiKey = getenv('SENDGRID_API_KEY');
$sg = new \SendGrid($apiKey);

$response = $sg->client->contactdb()->lists()->get();
$json_response = json_decode($response->body(), true);

foreach($json_response as $key){
    foreach($key as $type=>$value){

    $cat_id = $value['id'];
    $cat_name = $value['name'];
    $rec_count = $value['recipient_count'];
    echo "<option value='$cat_id'>$cat_name ($rec_count)</option>";

    $cat_id = $value['id'];
    $cat_name = $value['name'];
    $rec_count = $value['recipient_count'];
    $catid = $cat_id;
    $qry->free_result();
    $qry2 = $con->prepare("SELECT sg_id from email_campaign_categories where sg_id = ? ");
    echo $con->error;
    $qry2->bind_param("s", $catid);
    $qry2->execute();
    $qry2->store_result();
    if($qry2->num_rows < 1){
        $qry2->free_result();
        $ins_qry = $con->prepare("INSERT into email_campaign_categories(name,sg_id) VALUES(?,?)");
        $ins_qry->bind_param("ss", $catid, $catname);
        $catid = $cat_id;
        $catname = $cat_name;
        $ins_qry->execute();
    }
}

1 个答案:

答案 0 :(得分:0)

错误,&#34;错误:PHP致命错误:在3764行&#34;上的functions / functions.php中调用boolean上的成员函数bind_param(),告诉我们要查找的内容。

bind_param()在预期的查询(预准备语句)对象上调用,但它是boolean。这表明$con实体可能存在问题,因为对$con->prepare()的调用返回FALSE。