使用 PDO 获取单行单列 [英] Fetching single row, single column with PDO
问题描述
我有一个针对单行中单列的 mysql 查询
I have a mysql query that targets a single column in a single row
"SELECT some_col_name FROM table_name WHERE user=:user"
在我执行语句 $stmt->execute();
之后,我如何将这个单个单元格直接放入一个没有循环的变量中?换句话说,如何获得
After I execute the statement $stmt->execute();
how do I get this single cell directly placed into a variable with no loops? In other words how to get
from $stmt->execute();
to $col_value = 100;
我尝试了下面的 2,但都没有工作.. 该列在原始表中是 4,但我假设因为在我的 select 语句中我只选择它,当我指定参数时它应该是 1对于 fetchColumn.
I tried the 2 below, but neither worked.. The column is number 4 in the original table, but I'm assuming since in my select statement I'm selecting it only, it should be 1 when I specify the parameter for fetchColumn.
$col_value = $stmt->fetchColumn();
$col_value = $stmt->fetchColumn(0);
如您所见,我试图用尽可能少的行来完成.
As you can see, I'm trying to do it in as few lines as possible.
推荐答案
您确定它返回任何行吗?
Are you sure it's returning any rows?
$stmt->fetchColumn()
是获取单个值的正确方法,因此您可能没有绑定 :user 参数,或者它只是不返回任何行.
is correct way to fetch a single value, so either you probably didn't bind the :user parameter or it simply returned no rows.
这篇关于使用 PDO 获取单行单列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!