简体   繁体   English

使用新的mysqli查询获取多行?

[英]fetching multiple rows with new mysqli query?

I'm using php to connect to mysql database and am using the following function to retrieve multiple rows from database and then add them to an array and send them back. 我正在使用php连接到mysql数据库,并正在使用以下函数从数据库中检索多行,然后将它们添加到数组中并发送回去。 I'm getting internal server error or 500x error messages. 我收到内部服务器错误或500x错误消息。 I'm not sure what is wrong with this code. 我不确定这段代码有什么问题。 I've tried to figure it out but can't get it. 我试图弄清楚,但无法理解。

public function getUnreadMessages($uname){
          $stmt = $this->conn->prepare("SELECT msgid, title, fromuname FROM messages WHERE touname = ? and status = ?");
          $status = 'unread';
        $stmt->bind_param("ss", $uname, $status);
        if ($stmt->execute()) {
                $stmt->bind_result($col1, $col2, $col3);
                $stmt->store_result();
                $resp = array();
            while($stmt->fetch()){
                        $row = array();
                        array_push($row, $col1, $col2, $col3);
                        array_push($resp, $row);            
            }
            $msg = array('msgid' => $msgid,'title' => $title,'fromuname' => $fromuname);
            $stmt->close();
            return $msg;

        } else {
                        $stmt->close();
                     return NULL;
        }
    }

how should I go about doing this? 我应该怎么做呢? The function that handles the returned results are as follows 处理返回结果的函数如下

$app->get('/inbox', 'authenticate', function() use ($app){
        ob_start("ob_gzhandler");
        global $global_user_name;
        $db = new DbHandler();
        $resp = $db->getUnreadMessages($global_user_name);
        $msgs = array();
        $msgs['status'] = 'success';
        $msgs['version'] = 0.1;
        array_push($msgs, $resp);

        $app->response()->header('Content-Type', 'application/json');
        echo json_encode($msgs);
        exit;

});

The PDO method automating this is $stmt->fetchAll() :D 自动执行此操作的PDO方法是$ stmt-> fetchAll():D

public function getUnreadMessages($uname)
{
    $stmt = $this->conn->prepare("SELECT msgid,title,fromuname FROM messages WHERE touname = ? and status = ?");
    $stmt->execute([$uname, 'unread']))
    return $stmt->fetchAll(PDO::FETCH_ASSOC);
}

Return false if nothing has been found or something like : 如果未找到任何内容,则返回false

$msg = [
    [
        'msgid' => 2746,
        'title' => 'Random title',
        'fromuname' => 'SomeGuy'
    ],
    [
        'msgid' => 754,
        'title' => 'Some title',
        'fromuname' => 'Random Name'
    ],
    [
        'msgid' => 27686,
        'title' => 'Unreadable title',
        'fromuname' => 'Training dummies'
    ],
];

I've solved my question. 我已经解决了我的问题。 It was that I was creating an array within a loop. 那是我在循环内创建一个数组。 This seems to be disallowed. 这似乎是不允许的。 Here's the code that did work in the end 这是最后起作用的代码

public function getUnreadMessages($uname){

            $stmt = $this->conn->prepare("SELECT msgid, title, fromuname FROM messages WHERE touname = ? and status = ?");
                $status = 'unread';
                $stmt->bind_param("ss", $uname, $status);
            $result = $stmt->execute();
            $stmt->bind_result($col1, $col2, $col3);
            $stmt->store_result();
            $outer = array();
            $inner = array();
            while($stmt->fetch()){
                    array_push($inner, $col1, $col2, $col3);
            }
            array_push($outer, $inner);
            return $outer;
            $stmt->close();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM