我正在尝试动态绑定(bind)mysql_stmt参数,并在关联数组中获取结果。我在stackoverflow上找到了这篇文章,其中Amber用以下代码发布了答案:

原始帖子:
How to make a proper mysqli extension class with prepared statements?

“假设您实际上是想编写自己的版本(相对于利用现有库之一而言,其他答案已经提出了建议-这些也是不错的选择)...

这里有一些功能可能对您检查有用。第一个允许您将查询结果绑定(bind)到关联数组,第二个允许您传入两个数组,一个传递键的有序数组,另一个传递这些键的数据的关联数组并将该数据绑定(bind)到准备好的声明:”

function stmt_bind_assoc (&$stmt, &$out) {
    $data = mysqli_stmt_result_metadata($stmt);
    $fields = array();
    $out = array();

$fields[0] = $stmt;
$count = 1;

while($field = mysqli_fetch_field($data)) {
    $fields[$count] = &$out[$field->name];
    $count++;
}
call_user_func_array(mysqli_stmt_bind_result, $fields);

}

function stmt_bind_params($stmt, $fields, $data) {
    // Dynamically build up the arguments for bind_param
    $paramstr = '';
    $params = array();
    foreach($fields as $key)
    {
        if(is_float($data[$key]))
            $paramstr .= 'd';
        elseif(is_int($data[$key]))
            $paramstr .= 'i';
        else
            $paramstr .= 's';
        $params[] = $data[$key];
    }
    array_unshift($params, $stmt, $paramstr);
    // and then call bind_param with the proper arguments
    call_user_func_array('mysqli_stmt_bind_param', $params);
}

我尝试研究代码以了解其作用,并且使第二个功能正常工作,但我不知道要使用第一个功能应该怎么做。如何使用它检索类似于mysqli_result::fetch_assoc()的数组?

我希望能够像以前一样使用结果:
while ($row = mysql_fetch_array($result)){
  echo $row['foo']." ".$row['bar'];
}

请帮助我在此方面取得一些进展:)

最佳答案

好的,这是一种方法:

编辑,以修复获取多行时的错误

$sql = "SELECT `first_name`,`last_name` FROM `users` WHERE `country` =? AND `state`=?";
$params = array('Australia','Victoria');

/*
    In my real app the below code is wrapped up in a class
    But this is just for example's sake.
    You could easily throw it in a function or class
*/

// This will loop through params, and generate types. e.g. 'ss'
$types = '';
foreach($params as $param) {
    if(is_int($param)) {
        $types .= 'i';              //integer
    } elseif (is_float($param)) {
        $types .= 'd';              //double
    } elseif (is_string($param)) {
        $types .= 's';              //string
    } else {
        $types .= 'b';              //blob and unknown
    }
}
array_unshift($params, $types);

// Start stmt
$query = $this->connection->stmt_init(); // $this->connection is the mysqli connection instance
if($query->prepare($sql)) {

    // Bind Params
    call_user_func_array(array($query,'bind_param'),$params);

    $query->execute();

    // Get metadata for field names
    $meta = $query->result_metadata();

    // initialise some empty arrays
    $fields = $results = array();

    // This is the tricky bit dynamically creating an array of variables to use
    // to bind the results
    while ($field = $meta->fetch_field()) {
        $var = $field->name;
        $$var = null;
        $fields[$var] = &$$var;
    }


    $fieldCount = count($fieldNames);

// Bind Results
call_user_func_array(array($query,'bind_result'),$fields);

$i=0;
while ($query->fetch()){
    for($l=0;$l<$fieldCount;$l++) $results[$i][$fieldNames[$l]] = $fields[$fieldNames[$l]];
    $i++;
}

    $query->close();

    // And now we have a beautiful
    // array of results, just like
    //fetch_assoc
    echo "<pre>";
    print_r($results);
    echo "</pre>";
}

关于php - 动态绑定(bind)mysqli_stmt参数,然后绑定(bind)结果(PHP),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5300365/

10-12 00:04
查看更多