使用 PDO 获取单个返回值


Get the single returned value with PDO

对于此查询: $sql='select col from table where other_col=?';

我目前正在获取返回的数据,如下所示: $data=$statement->fetch()['col'];

这对我来说看起来不太好。有没有更好的方法,或者这样很好?

你可以试试:

$data=$statement->fetchColumn(0);