php - 动态绑定(bind)mysqli_stmt参数再绑定(bind)结果

标签 php mysqli prepared-statement

我正在尝试动态绑定(bind) mysql_stmt 参数并在关联数组中获取结果。我在 Stack Overflow 上找到了这篇文章,其中 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)结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5300365/

相关文章:

php - 使用 Range 从两个表中获取数据

php - 使用准备好的语句将相同的数据插入到两个表中

javascript - jquery mobile ajax 将内容加载到 div 元素中会丢失它的 css 样式

javascript - opencart 2.0 未定义 gaq

php - Eloquent "select"方法不适用于使用 "with"方法

javascript - 当我的 html 中的一列有两个不同的名称时,如何在数据库中插入数据?

php - PDO连接类/代码和类设计

php - jquery ajax 即使发生更新也总是出错

Mysql自增Id在使用prepared statements时不按顺序

Java MySQL preparedStatement 批处理