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

The*_*ile 1 php mysql mysqli stdclass

我需要数据库表中最后一个有效的条目,它将是具有最大主键的行.所以使用mysqli,我的查询是"SELECT MAX(id)FROM table LIMIT 1".此查询返回正确的数字(使用print_r()),但我无法弄清楚如何访问它.这是主要代码.请注意,$ this->链接指的是具有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
    }
Run Code Online (Sandbox Code Playgroud)

有效行print_r($ row)回显"stdClass Object([max(id)] => 68)"

nik*_*org 5

您需要命名聚合结果.

SELECT MAX(id) AS maxid FROM stones
Run Code Online (Sandbox Code Playgroud)

然后你可以访问像这样的值$row->maxid.