Mysqli-将结果绑定到数组 [英] Mysqli - Bind results to an Array

查看:62
本文介绍了Mysqli-将结果绑定到数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近从mysql切换到mysqli,并开始使用准备好的语句. 在mysql中,我们这样做

I recently switched to mysqli from mysql and started using prepared statements. in mysql we do

$result = mysql_query("SELECT * FROM table WHERE id = ?");

所以我们将整个表的数组放在一个变量中.

So we get the array of whole table in one variable.

但是在mysqli中,我们这样做

But in mysqli we do

mysqli_stmt_bind_result($stmt, $result);

因此,基本上,这里只有一个变量绑定到变量结果. 如何获得与mysql相同的变量(数组)?
PS-希望我的问题足够清楚.我知道他们的方法不多,但我希望能找到最好的方法.
PSS-我对程序方式比较满意.

so basically here only one variable gets bind to variable result. How can we get the same variable(array) that we got from mysql?
PS - Hope my ques is clear enough. I know their are not many methods possible but i am luking for the best one.
PSS - I am more comfortable with the procedural way.

推荐答案

最后,这段代码正在工作!!!!

Finally this code is Working !!!!!

  <?php 
    $host = 'localhost'; 
    $user = 'root'; 
    $pass = '1234'; 
    $data = 'test'; 

    $mysqli = new mysqli($host, $user, $pass, $data); 
    /* check connection */ 
    if (mysqli_connect_errno()) { 
        printf("Connect failed: %s\n", mysqli_connect_error()); 
        exit(); 
    } 

    if ($stmt = $mysqli->prepare("SELECT * FROM sample WHERE t2 LIKE ?")) { 
        $tt2 = '%'; 

        $stmt->bind_param("s", $tt2); 
        $stmt->execute(); 

        $meta = $stmt->result_metadata(); 
        while ($field = $meta->fetch_field()) 
        { 
            $params[] = &$row[$field->name]; 
        } 

        call_user_func_array(array($stmt, 'bind_result'), $params); 

        while ($stmt->fetch()) { 
            foreach($row as $key => $val) 
            { 
                $c[$key] = $val; 
            } 
            $result[] = $c; 
        } 

        $stmt->close(); 
    } 
    $mysqli->close(); 
    print_r($result); 
    ?>

这篇关于Mysqli-将结果绑定到数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆