簡體   English   中英

如何訪問stdClass變量stdClass對象([max(id)])=> 64)

[英]How to access stdClass variables stdClass Object([max(id)])=>64)

我需要數據庫表中的最后一個有效條目,該表將是具有最大主鍵的行。 因此,使用mysqli,我的查詢是“從表LIMIT 1選擇SELECT MAX(id)”。 該查詢返回正確的數字(使用print_r()),但我不知道如何訪問它。 這是主要代碼。 請注意,$ this-> link引用具有mysqli連接的類。

$q="select max(id) from stones limit 1";
    $qed=$this->link->query($q) or die(mysqli_error());
    if($qed){
        $row=$qed->fetch_object();
        print_r($row);
        echo $lastid=$row;//here is the problem
    }

有效行print_r($ row)回顯“ stdClass對象([max(id)] => 68)”

您需要命名匯總結果。

SELECT MAX(id) AS maxid FROM stones

然后,您可以訪問$row->maxid類的值。

我需要數據庫表中的最后一個有效條目,該表將是具有最大主鍵的行。

您說您想要最后一個條目,但只獲取ID。 大概您打算使用它來通過第二個查詢來獲取整個行。

相反,您可以在一個查詢中完成整個操作:

SELECT *
FROM stones
ORDER BY id DESC
LIMIT 1

你有沒有嘗試過 :

$ row-> max(id)? 或$ lastid = $ row [“ max(id)”];

您可能需要選擇一個max(id)作為“ MaxID”,然后選擇$ lastid = $ row-> MaxID;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM