【问题标题】:Retrieving array with mysqli使用 mysqli 检索数组
【发布时间】:2014-07-29 20:05:18
【问题描述】:

我正在尝试将一些使用 mysql 的旧 php 代码转换为使用 mysqli,

我更新的代码是:

function mysql_fetch_full_result_array($result){
global $dbc;

    $table_result=array();
    $r=0;
    while($row = mysqli_fetch_assoc($result)){
        $arr_row=array();
        $c=0;
        while ($c < mysqli_num_fields($result)) {       
            $col = mysqli_fetch_field($result);   
            $arr_row[$col -> name] = $row[$col -> name];           
            $c++;
        }   
        $table_result[$r] = $arr_row;
        $r++;
    }   
    return $table_result;
}

但是,在第一行之后,它只为这里看到的每个值输出 null,我确定我没有增加一些东西,但我不确定此时在哪里或如何:

{"DOMAIN.COM":[{"image":"http:\/\/DOMAIN.COM\/photos\/grid_logo.jpg","source":"logo","user":"","printed":"","note":""},{"image":"http:\/\/DOMAIN.COM\/photos\/grid_block.jpg","source":"block","user":"","printed":"","note":""},{"image":"http:\/\/pbs.twimg.com\/media\/BlNvdcrCAAAlz8-.jpg","source":"twitter","user":"ObserveMeditate","printed":"0","note":"RT @IMSEngage: Moby (@thelittleidiot) sits down with the legendary @DAVID_LYNCH this Wednesday: http:\/\/t.co\/PMn9EHQVcp #IMSEngage http:\/\/t.\u2026"},{"image":null,"source":null,"user":null,"printed":null,"note":null},{"image":null,"source":null,"user":null,"printed":null,"note":null},{"image":null,"source":null,"user":null,"printed":null,"note":null},{"image":null,"source":null,"user":null,"printed":null,"note":null},{"image":null,"source":null,"user":null,"printed":null,"note":null},{"image":null,"source":null,"user":null,"printed":null,"note":null},{"image":null,"source":null,"user":null,"printed":null,"note":null},{"image":null,"source":null,"user":null,"printed":null,"note":null},{"image":null,"source":null,"user":null,"printed":null,"note":null},{"image":null,"source":null,"user":null,"printed":null,"note":null},{"image":null,"source":null,"user":null,"printed":null,"note":null},{"image":null,"source":null,"user":null,"printed":null,"note":null},{"image":null,"source":null,"user":null,"printed":null,"note":null},{"image":null,"source":null,"user":null,"printed":null,"note":null},{"image":null,"source":null,"user":null,"printed":null,"note":null},{"image":null,"source":null,"user":null,"printed":null,"note":null},{"image":null,"source":null,"user":null,"printed":null,"note":null},{"image":null,"source":null,"user":null,"printed":null,"note":null},{"image":null,"source":null,"user":null,"printed":null,"note":null},{"image":null,"source":null,"user":null,"printed":null,"note":null}]}

【问题讨论】:

    标签: php mysql arrays mysqli


    【解决方案1】:

    看来您正在尝试做的事情与此相同(除了大量无关代码):

    while($row = mysqli_fetch_assoc($result)) {
        $table_result[] = $row;
    }   
    return $table_result;
    

    或许:

    while($row[] = mysqli_fetch_assoc($result)) { }   
    return $row;
    

    甚至只是:

    $all_rows = mysqli_fetch_all($result, MYSQLI_ASSOC); //may still be server dependent
    

    【讨论】:

    • 谢谢。我用了你的第一个,这就是我所需要的。我很感激。
    • 如果可以,我会使用mysqli_fetch_all
    猜你喜欢
    • 2018-01-06
    • 2012-06-25
    • 2010-10-12
    • 1970-01-01
    • 2016-10-01
    • 2017-07-11
    • 2015-01-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多