第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

php + mysql 查詢僅從類函數(shù)返回單行(標準類)

php + mysql 查詢僅從類函數(shù)返回單行(標準類)

PHP
SMILET 2022-09-25 20:50:06
你能告訴我為什么這只返回我的查詢的最后一行嗎?正如你所看到的,我正在提取作為標準類。此外,我已經(jīng)嘗試了不同的方法,例如在內(nèi)部使用foreach key=>值,但它沒有幫助。我無法正確填充$out。class myclass {    function Query($sql){    $results = $this->db->query($sql);    if (mysqli_num_rows($results)<1){           throw new Exception('NoResults');           }       $out = new stdClass;            while ($r = $results->fetch_object()){        $out = $r;      }     return $out;    $out = null;    }}}---------------$client = new myclass;    $sql = "SELECT * FROM books";    $q = $client->Query($sql);    print_r($q);
查看完整描述

3 回答

?
精慕HU

TA貢獻1845條經(jīng)驗 獲得超8個贊

您只需要更改這些行:


$out = new stdClass;        

while ($r = $results->fetch_object()){

    $out = $r;  

對那些:


$out = []; // array that will hold all the objects

while ($r = $results->fetch_object()){

    array_push($out, $r);  // add to the array the current object

return $out; //return the array with the objects


查看完整回答
反對 回復 2022-09-25
?
慕勒3428872

TA貢獻1848條經(jīng)驗 獲得超6個贊

您將在 的每次迭代中覆蓋 ,因此在返回中只有最后一個結果。您可以使用數(shù)組并追加結果(它可以是stdClass對象的數(shù)組),然后您將能夠使用一個簡單的循環(huán)來處理它$outwhile


class myclass {


    function Query($sql){


        $results = $this->db->query($sql);


        if (mysqli_num_rows($results)<1){   

            throw new Exception('NoResults');       

        }   


        //copied this piece of code from @Berto99 answer from this same question

        $out = []; // array that will hold all the objects

        while ($r = $results->fetch_object()){

            array_push($out, $r);  // add to the array the current object

        } 

        return $out; //return the array with the objects

    }  


}



---------------


$client = new myclass;


$sql = "SELECT * FROM books";

$q = $client->Query($sql);



foreach($q as $resultLine){

    //do whatever you need to do here

}


查看完整回答
反對 回復 2022-09-25
?
莫回無

TA貢獻1865條經(jīng)驗 獲得超7個贊

你的$r是對象。您不需要標準類。您需要將對象添加到$out數(shù)組中。


function Query($sql)

{

    $results = $this->db->query($sql);


    if (mysqli_num_rows($results) < 1) {   

       throw new Exception('NoResults');       

    }   


    $out = new stdClass;

    $i=0;

    while ($r = $results->fetch_object()){

        $out->{$i} = $r;  

        $i++

    } 


    return $out;

}


查看完整回答
反對 回復 2022-09-25
  • 3 回答
  • 0 關注
  • 110 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號