尝试使用stmt-> bind_result从选项中获取值;导致致命错误(内存不足)

时间:2014-09-18 14:40:52

标签: php mysqli

就像标题所说,我正在尝试使用mysqli fetch stmt从数据库中获取meta_keys和meta_values。我得到的错误是说内存不足,并在我的代码中引用了这一行:

//Bind result variables
$select_stmt->bind_result($id, $key, $value);

我不确定我做错了哪里或哪里,这就是整个功能。我的目标是在另一个文件中调用该函数,并传递我想要检索meta_value的userID和选项(meta_key)。

function get_options($userid, $option) {
    global $mysqli;


    $query = "SELECT user_id, meta_key, meta_value FROM user_settings WHERE user_id = ? AND meta_key = ? LIMIT 4";

    if($select_stmt = $mysqli->prepare($query)) {

        $select_stmt->bind_param('ss', $userid, $option);

        //Execute the prepared statement
        $select_stmt->execute();

        //Bind result variables
        $select_stmt->bind_result($id, $key, $value);

        //Fetch the values
        while ($select_stmt->fetch()) {
            printf($key, $value);
        }

    }
}

1 个答案:

答案 0 :(得分:0)

我明白了,这对我有用,希望能帮助其他人:

function get_options($userid, $option) {
    global $mysqli;

    if ($select_stmt = $mysqli->prepare("SELECT user_id, meta_key, meta_value, salt 
          FROM user_settings 
          WHERE user_id = ? AND meta_key = ?")) {
        $select_stmt->bind_param('ss', $userid, $option);
        $select_stmt->execute();   // Execute the prepared query.
        $select_stmt->store_result();

        // Execute the prepared query.
        if (! $select_stmt->execute()) {
            echo 'Could not perform option query on database';
        }

        // If the user exists get variables from result.
        $select_stmt->bind_result($userID, $key, $value, $salt);
        $select_stmt->fetch();

        echo ' ID is '.$userID.'<br />';
        echo ' Key is '.$key.'<br />';
        echo ' Value is '.$value.'<br />';
        echo ' Salt is '.$salt.'<br />';


    } //end if($select_stmt) 


}

在上面你会看到我在绑定结果之前添加了store_result,并在绑定结果后添加了fetch属性。