簡體   English   中英

使用新的mysqli查詢獲取多行?

[英]fetching multiple rows with new mysqli query?

我正在使用php連接到mysql數據庫,並正在使用以下函數從數據庫中檢索多行,然后將它們添加到數組中並發送回去。 我收到內部服務器錯誤或500x錯誤消息。 我不確定這段代碼有什么問題。 我試圖弄清楚,但無法理解。

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;
        }
    }

我應該怎么做呢? 處理返回結果的函數如下

$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;

});

自動執行此操作的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);
}

如果未找到任何內容,則返回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'
    ],
];

我已經解決了我的問題。 那是我在循環內創建一個數組。 這似乎是不允許的。 這是最后起作用的代碼

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