【问题标题】:Returning Multiple Rows with MySqli and Arrays使用 MySqli 和数组返回多行
【发布时间】:2023-03-12 19:19:01
【问题描述】:

过去两天左右,我一直在将函数转换为 mysqli。我遇到了一个问题。我有一个函数,它返回一个包含数据库中一行的数组。但是,我希望数组包含多行而不是一行。此外,我将如何回应各个帖子。这是我的失败尝试,只显示数组中的一行。

$mysqli = new mysqli("localhost", "user", "password", "database");   

function display_posts ($mysqli, $user_id) {

   $fields = "`primary_id`, `poster_id`, `profile_user_id`, `post`";   

   $user_id = (int)$user_id;

   $query = "SELECT DISTINCT $fields FROM `posts` WHERE `profile_user_id` = $user_id 
   LIMIT 4";                    

   if ($result = $mysqli->query($query)) {

   $row = $result->fetch_assoc();

   return $row;

   $result->free();

   $stmt->close();

}}

我在这里尝试显示数据。

$user_id = 1;

$posts = display_posts($mysqli, $user_id);

//Not sure what to do with $posts. A While loop perhaps to display each post?

【问题讨论】:

    标签: php arrays mysqli


    【解决方案1】:

    你必须使用循环来一次获取它们:

    <?php
    function resultToArray($result) {
        $rows = array();
        while($row = $result->fetch_assoc()) {
            $rows[] = $row;
        }
        return $rows;
    }
    
    // Usage
    $query = 'SELECT DISTINCT $fields FROM `posts` WHERE `profile_user_id` = $user_id LIMIT 4';
    $result = $mysqli->query($query);
    $rows = resultToArray($result);
    var_dump($rows); // Array of rows
    $result->free();
    

    【讨论】:

    • 虽然我希望得到一个答案,如何回应每个单独的价值,但我继续前进并自己弄清楚了。尽管如此,还是感谢您的帮助。我决定取出函数resultToArray 并将其附加到我原来的函数中。
    • 最糟糕的做法!!,当MYSQL默认可以做到这一点时,为什么要把工作交给PHP。请参阅下面的答案。
    【解决方案2】:

    为什么不直接这样使用:

    $result = mysqli_fetch_all($mysqli->query($query), MYSQLI_ASSOC);
    

    【讨论】:

    • @KateGregory 实际上没有什么可解释的。除了这个函数只在 5.3 以后才可用并且只在 mysqlnd 下可用。
    • 没什么好解释的。与其做一个 while 循环,不如使用可用的 mysqli 方法来获取整个结果集。也避免为它声明一个额外的函数。是的,它> 5.3。但据我所知,mysqli 也是 5.3 及更高版本
    【解决方案3】:

    我迟到了,但我相信这是您想要实现的目标:

    $mysqli = new mysqli("localhost", "user", "password", "database");
    $fields = "`primary_id`, `poster_id`, `profile_user_id`, `post`";
    
    function display_posts () {
    
        global $mysqli;
        global $fields;
    
        $query = "SELECT DISTINCT $fields FROM `posts` WHERE `profile_user_id` = $user_id LIMIT 4";
    
        $posts = $mysqli -> query($query) or die('Error: '.$mysqli -> error);
    
        if ($posts -> num_rows > 0) {
    
            while ($row = $posts -> fetch_assoc()) {
    
            $value = $row['/*The table column here (You can repeat this line with a different variable e.g. $value 2, $value 3 etc and matching them with the respective table column)*/'];
    
            echo $value./*Concatenate the other variables ($value 1 etc) here*/'<br />';
    
            }
    
        }else {
    
            echo 'No records found.';
    
        }
    
    }
    
    //Call the function
    display_posts ();
    

    【讨论】:

    • 你不想让你的 $mysqli 全球化。这是一个巨大的安全风险。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多