当前位置: 首页 > 知识库问答 >
问题:

函数不工作:PHP致命错误:在布尔值上调用成员函数bind_param()

越星晖
2023-03-14

这是我的函数,我在这里坐了很长时间,试图找出什么不起作用,它说它在$stmt2的行上-

function addDailyTickets() {
    global $mysqli,$db_table_prefix;

    $stmt = $mysqli->prepare("SELECT 
        id
        FROM ".$db_table_prefix."users 
        WHERE active=1
        ");

    $stmt->execute();
    $stmt->bind_result($id);

    while ($stmt->fetch()){
        $created_timestamp = time();
        $status = "active";
        $transaction_id = "daily_".date('n-j-Y');
        $game_id = 0;
        $ticket_uniqID = uniqid("tick_");

        $stmt2 = $mysqli->prepare("INSERT INTO ".$db_table_prefix."tickets (
        user_id,
        created_timestamp,
        status,
        transaction_id,
        game_id,
        ticket_uniqID
        )
        VALUES (
        ?,
        ?,
        ?,
        ?,
        ?,
        ?
        )");
        $stmt2->bind_param("iissis", $id, $created_timestamp, $status, $transaction_id, $game_id, $ticket_uniqID);
        $stmt2->execute();
    }
    $stmt->close();
    $stmt2->close();    
}

如果我放一个$stmt2-

共有1个答案

杜楚
2023-03-14

好的,多亏了@Charlotte Dunois,我明白了,我必须将第一个mysql调用放入一个数组中,然后关闭它,然后调用第二个:

function addDailyTickets() {
    global $mysqli,$db_table_prefix;

    $stmt = $mysqli->prepare("SELECT 
        id
        FROM ".$db_table_prefix."users 
        WHERE active=1
        ");

    $stmt->execute();
    $stmt->bind_result($id);
    while ($stmt->fetch()){
        $row[] = $id;
    }
    $stmt->close();

    foreach($row as $id){
        $created_timestamp = time();
        $status = "active";
        $transaction_id = "daily_".date('n-j-Y');
        $game_id = 0;
        $ticket_uniqID = uniqid("tick_");

        $stmt2 = $mysqli->prepare("INSERT INTO ".$db_table_prefix."tickets (
        user_id,
        created_timestamp,
        status,
        transaction_id,
        game_id,
        ticket_uniqID
        )
        VALUES (
        ?,
        ?,
        ?,
        ?,
        ?,
        ?
        )");
        $stmt2->bind_param("iissis", $id, $created_timestamp, $status, $transaction_id, $game_id, $ticket_uniqID);
        $stmt2->execute();
    }
    $stmt2->close();
}
 类似资料: