php 使用 PDO 获取单行单列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1666710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:26:51  来源:igfitidea点击:

Fetching single row, single column with PDO

phpmysqldatabasepdo

提问by Chris

I have a mysql query that targets a single column in a single row

我有一个针对单行中的单列的 mysql 查询

"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

执行语句后,$stmt->execute();如何将这个单个单元格直接放入没有循环的变量中?换句话说,如何获得

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.

我尝试了下面的 2,但都没有工作.. 该列在原始表中是 4,​​但我假设因为在我的 select 语句中我只选择它,当我为 fetchColumn 指定参数时它应该是 1。

$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.

正如你所看到的,我试图用尽可能少的行来完成。

回答by reko_t

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.

你确定它返回任何行吗?
$stmt->fetchColumn()
是获取单个值的正确方法,因此您可能没有绑定 :user 参数,或者它只是不返回任何行。

回答by JAL

$sql='SELECT some_col_name FROM table_name WHERE user=?';

$sth=$pdo_dbh->prepare($sql);
$data=array($user);

$sth->execute($data);

$result=$sth->fetchColumn();

回答by Matthew

I'm not sure why so many people mess this up:

我不知道为什么这么多人搞砸了:

$stmt = $dbh->prepare("SELECT `column` FROM `table` WHERE `where`=:where"); 
$stmt->bindValue(':where', $MyWhere);
$stmt->execute();
$SingleVar = $stmt->fetchColumn();

Make sure that you are selecting a specific columnin the query and not *or you will need to specify the column order number in fetchColumn(), example: $stmt->fetchColumn(2);That usually isn't a good idea because the columns in the database may be reorganized by, someone...

确保您column在查询中选择了特定的而不是,*否则您将需要在 中指定列顺序号fetchColumn(),例如:$stmt->fetchColumn(2);这通常不是一个好主意,因为数据库中的列可能被某人重新组织...

This will only work properly with unique 'wheres'; fetchColumn()will not return an array.

这只适用于unique 'wheres'; fetchColumn()不会返回数组。

回答by Cold Pen

When you want to get the last insert you add the DESC Limit 1 to the sql statement.

当您想要获得最后一个插入时,您可以将 DESC Limit 1 添加到 sql 语句中。

$sql = "SELECT `some_col_name` FROM table_name\n"

    . "ORDER BY `some_col_name` DESC\n"

    . "LIMIT 1";

$stmt = $conn->prepare($sql);
$result = $stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
//convert the array content to string and store in variable 
$col = implode(" ", $row);
echo $col;

回答by 1nstinct

You could use this:

你可以用这个:

$stmt->fetch(PDO::FETCH_COLUMN, $number_of_column);

回答by True Soft

Have you prepared the statement first? (Before $stmt->execute())

你先准备好声明了吗?(之前$stmt->execute()

$stmt = $db->prepare("SELECT some_col_name FROM table_name WHERE user=:user");