简体   繁体   English

如何访问stdClass变量stdClass对象([max(id)])=> 64)

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

I need the very last valid entry in a database table which would be the row with the greatest primary key. 我需要数据库表中的最后一个有效条目,该表将是具有最大主键的行。 So using mysqli, my query is "SELECT MAX(id) FROM table LIMIT 1". 因此,使用mysqli,我的查询是“从表LIMIT 1选择SELECT MAX(id)”。 This query returns the correct number(using print_r()) but I cannot figure out how to access it. 该查询返回正确的数字(使用print_r()),但我不知道如何访问它。 Here is the main code. 这是主要代码。 Note that the $this->link refers to class with a mysqli connection. 请注意,$ 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
    }

The valid line print_r($row) echos out "stdClass Object ( [max(id)] => 68 )" 有效行print_r($ row)回显“ stdClass对象([max(id)] => 68)”

You need to name the aggregate result. 您需要命名汇总结果。

SELECT MAX(id) AS maxid FROM stones

Then you can access the value like $row->maxid . 然后,您可以访问$row->maxid类的值。

I need the very last valid entry in a database table which would be the row with the greatest primary key. 我需要数据库表中的最后一个有效条目,该表将是具有最大主键的行。

You say you want the last entry but you're only fetching the ID. 您说您想要最后一个条目,但只获取ID。 Presumably you intend to use this to fetch the entire row with a second query. 大概您打算使用它来通过第二个查询来获取整个行。

Instead you could do the whole operation in one query: 相反,您可以在一个查询中完成整个操作:

SELECT *
FROM stones
ORDER BY id DESC
LIMIT 1

Have you tried : 你有没有尝试过 :

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

You may need to do a select max(id) as "MaxID" and $lastid=$row->MaxID; 您可能需要选择一个max(id)作为“ MaxID”,然后选择$ lastid = $ row-> MaxID;

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

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