获取单行,单列与PDO [英] Fetching single row, single column with PDO
问题描述
SELECT some_col_name FROM table_name WHERE user =:用户
执行语句 $ stmt-> execute ;
如何获得这个单元格直接放入一个没有循环的变量?换句话说,如何从$ stmt-> execute()获取
to $ col_value = 100;
我尝试了下面的2,但都没有工作..列是原始表中的数字4,但我假设,因为在我的选择语句中,我只选择它,当我指定fetchColumn的参数应该是1。
$ col_value = $ stmt-> fetchColumn
$ col_value = $ stmt-> fetchColumn(0);
正如你所看到的,我试图尽可能少的行。
确定要返回任何行吗?
$ stmt-> fetchColumn()
是获取单一值的正确方法,因此您可能没有绑定:user参数,或者它只是返回没有行。
I have a mysql query that targets a single column in a single row
"SELECT some_col_name FROM table_name WHERE user=:user"
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;
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()
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屋!