显示 mysqli 查询中单行的单列值

问题描述 投票:0回答:3

如何从 mysqli 获取单个列值?结果应该是单行只有一列。

这是我尝试过的:

$query = "SELECT MAX(`userid`) FROM `user`";
$rlt = mysqli_query($this->db, $query);
echo $rlt['userid'];
php mysql mysqli
3个回答
2
投票

执行查询后您没有获取行:

$query = "SELECT MAX(`userid`) FROM `user'";
$rlt = mysqli_query($this->db,$query);
$row = mysqli_fetch_row($this->db, $rlt);
echo $row[0];

另一种方法是为计算字段使用别名并使用

fetch_assoc
:

$query = "SELECT MAX(`userid`) as `maxid` FROM `user'";
$rlt = mysqli_query($this->db,$query);
$row = mysqli_fetch_assoc($this->db, $rlt);
echo $row['maxid'];

0
投票

尝试创建别名并在查询执行后获取结果,你的引用

user'
看起来错误

$query = "SELECT MAX(`userid`) as userid  FROM `user`";
$rlt = mysqli_query($this->db,$query);
$r = mysqli_fetch_assoc($rlt);
echo $r['userid'];

或者只获取一行,例如:-

$r = mysqli_fetch_row($this->db, $rlt);
echo $r[0];

-1
投票

您应该在此处创建

alias

使用这个:

$query = "SELECT MAX(`userid`) as Max_Userid  FROM `user'";
$rlt = mysqli_query($this->db,$query);
$row = mysqli_fetch_assoc($this->db, $rlt);
echo $row['Max_Userid'];

注意: 使用 mysql 函数进行字段查询时,请务必使用别名。

© www.soinside.com 2019 - 2024. All rights reserved.