在php中显示mysqli查询的单列值

时间:2014-05-27 10:58:16

标签: php mysql

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

这就是我的尝试

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

3 个答案:

答案 0 :(得分: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'];

答案 1 :(得分:0)

尝试使用create alias并在查询执行后获取结果以及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];

答案 2 :(得分:-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函数时始终使用别名。