在 php mysqli 准备好的语句中将数据附加到数组中

发布于 2024-10-07 16:42:43 字数 529 浏览 5 评论 0原文

我很累在这里写一个简单的函数。这个想法是能够调用函数并显示数据,但我想知道是否有一种方法可以将其填充到某种数组或其他东西中,以便我可以在调用函数时设置结果的样式。函数如下:

function getDBH() {
    static $DBH = null;
    if (is_null($DBH)) {
        $DBH = new mysqli(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
    }
    return $DBH;
}
function selectInfo($limit, $offset){
    $DBH = getDBH();
    $stmt = $DBH->prepare("SELECT * FROM information LIMIT ?,?");
    $stmt->bind_param("ii", $limit, $offset);
    $stmt->execute();
    $stmt->bind_result($id, $name, $email);
}

I am tyring to write a simple function here. The idea is to be able to call the function and display the data, but i am wondering if there is a way to stuff it into an array of some sort or something so that i can style the results when the function is called. the function is as follows:

function getDBH() {
    static $DBH = null;
    if (is_null($DBH)) {
        $DBH = new mysqli(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
    }
    return $DBH;
}
function selectInfo($limit, $offset){
    $DBH = getDBH();
    $stmt = $DBH->prepare("SELECT * FROM information LIMIT ?,?");
    $stmt->bind_param("ii", $limit, $offset);
    $stmt->execute();
    $stmt->bind_result($id, $name, $email);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

内心荒芜 2024-10-14 16:42:43

selectInfo() 函数的替换应返回一个多维数组,其中每条记录包含一个关联数组。它不依赖于您事先知道语句中返回哪些字段,因为如果您以任何方式更改表,则 select 语句中的 * 很容易破坏内容。

function selectInfo($limit, $offset) {
    $DBH = getDBH();
    $limit = (int) $limit;
    $offset= (int) $offset;
    $stmt = $DBH->prepare("SELECT * FROM information LIMIT ?,?");
    $stmt->bind_param("ii", $limit, $offset);
    $stmt->execute();
    // get all field info in returned set
    $result_metadata = $stmt->result_metadata();
    $result_fields = $result_metadata->fetch_fields();  
    $result_metadata->free_result();
    unset($result_metadata);
    $bind_array = array();
    $current_row = array();
    $all_rows = array();
    foreach($result_fields as $val) {
        // use returned field names to populate associative array
        $bind_array[$val->name] = &$current_row[$val->name];
        }
    // bind results to associative array
    call_user_func_array(array($stmt, "bind_result"), $bind_array);
    // continually fetch all records into associative array and add it to final $all_rows array
    while($stmt->fetch()) $all_rows[] = $current_row;
    return $all_rows;
    }

编辑:稍微更改了您的selectInfo()函数,因为我之前的答案太草率了。 print_r(selectInfo(0,10)); 将打印出字段名称完整的所有记录。

This replacement for your selectInfo() function should return a multi-dimensional array containing one associative array per record. It does not rely on you knowing before hand what fields are returned in the statement as that * in the select statement can otherwise easily break things if you alter your table in any way.

function selectInfo($limit, $offset) {
    $DBH = getDBH();
    $limit = (int) $limit;
    $offset= (int) $offset;
    $stmt = $DBH->prepare("SELECT * FROM information LIMIT ?,?");
    $stmt->bind_param("ii", $limit, $offset);
    $stmt->execute();
    // get all field info in returned set
    $result_metadata = $stmt->result_metadata();
    $result_fields = $result_metadata->fetch_fields();  
    $result_metadata->free_result();
    unset($result_metadata);
    $bind_array = array();
    $current_row = array();
    $all_rows = array();
    foreach($result_fields as $val) {
        // use returned field names to populate associative array
        $bind_array[$val->name] = &$current_row[$val->name];
        }
    // bind results to associative array
    call_user_func_array(array($stmt, "bind_result"), $bind_array);
    // continually fetch all records into associative array and add it to final $all_rows array
    while($stmt->fetch()) $all_rows[] = $current_row;
    return $all_rows;
    }

Edit: Changed up your selectInfo() function a bit as my previous answer was just too sloppy. print_r(selectInfo(0,10)); will print out all records with field names intact.

九八野马 2024-10-14 16:42:43
$stmt->bind_result($id, $name, $email);

while (mysqli_stmt_fetch($stmt)) {
    $row = array('id' => $id, 'name' => $name, 'email' => $email);
    $rows[] = $row;
}

return $rows;
$stmt->bind_result($id, $name, $email);

while (mysqli_stmt_fetch($stmt)) {
    $row = array('id' => $id, 'name' => $name, 'email' => $email);
    $rows[] = $row;
}

return $rows;
淡淡離愁欲言轉身 2024-10-14 16:42:43

使用 !而不是?附加静态数据。 ?附加引用的转义数据。 !不安全,但不限。

Use ! instead of ? to attach static data. ? attaches quoted, escaped data. ! is unsafe, but not for limit.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文