PHP PDO 返回单行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5456626/
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 21:30:17  来源:igfitidea点击:

PHP PDO returning single row

phppdo

提问by oshirowanen

UPDATE 2:

更新 2:

So is this the most optimized it can get?

那么这是它可以得到的最优化的吗?

$DBH = new PDO( "connection string goes here" );

$STH = $DBH -> prepare( "select figure from table1" );

$STH -> execute();

$result = $STH -> fetch();

echo $result ["figure"];

$DBH = null;

UPDATE 1:

更新1:

I know I can add limit to the sql query, but I also want to get rid of the foreach loop, which I should not need.

我知道我可以为 sql 查询添加限制,但我也想摆脱我不需要的 foreach 循环。

ORIGINAL QUESTION:

原问题:

I have the following script which is good IMO for returning many rows from the database because of the "foreach" section.

我有以下脚本,由于“foreach”部分,它非常适合从数据库中返回许多行。

How do I optimize this, if I know I will always only get 1 row from the database. If I know I will only ever get 1 row from the database, I don't see why I need the foreach loop, but I don't know how to change the code.

如果我知道我总是只能从数据库中获得 1 行,我该如何优化它。如果我知道我只会从数据库中获取 1 行,我不明白为什么我需要 foreach 循环,但我不知道如何更改代码。

$DBH = new PDO( "connection string goes here" );

$STH = $DBH -> prepare( "select figure from table1" );

$STH -> execute();

$result = $STH -> fetchAll();

foreach( $result as $row ) {
    echo $row["figure"];
}

$DBH = null;

回答by mjspier

Just fetch. only gets one row. So no foreach loop needed :D

就取。只得到一排。所以不需要 foreach 循环:D

$row  = $STH -> fetch();

example (ty northkildonan):

示例(ty northkildonan):

$dbh = new PDO(" --- connection string --- "); 
$stmt = $dbh->prepare("SELECT name FROM mytable WHERE id=4 LIMIT 1"); 
$stmt->execute(); 
$row = $stmt->fetch();

回答by strauberry

$DBH = new PDO( "connection string goes here" );
$STH - $DBH -> prepare( "select figure from table1 ORDER BY x LIMIT 1" );

$STH -> execute();
$result = $STH -> fetch();
echo $result ["figure"];

$DBH = null;

You can use fetch and LIMIT together. LIMIT has the effect that the database returns only one entry so PHP has to handle very less data. With fetch you get the first (and only) result entry from the database reponse.

您可以同时使用 fetch 和 LIMIT。LIMIT 的作用是数据库只返回一个条目,因此 PHP 必须处理的数据非常少。通过 fetch,您可以从数据库响应中获得第一个(也是唯一一个)结果条目。

You can do more optimizing by setting the fetching type, see http://www.php.net/manual/de/pdostatement.fetch.php. If you access it only via column names you need to numbered array.

您可以通过设置获取类型进行更多优化,请参阅http://www.php.net/manual/de/pdostatement.fetch.php。如果仅通过列名访问它,则需要对数组进行编号。

Be aware of the ORDER clause. Use ORDER or WHERE to get the needed row. Otherwise you will get the first row in the table alle the time.

请注意 ORDER 子句。使用 ORDER 或 WHERE 获取所需的行。否则,您将始终获得表中的第一行。

回答by Bé Kh?e Bé Pro

Did you try:

你试过了吗:

$DBH = new PDO( "connection string goes here" );
$row = $DBH->query( "select figure from table1" )->fetch();
echo $row["figure"];
$DBH = null;

回答by user3162468

You could try this for a database SELECT query based on user input using PDO:

您可以尝试使用 PDO 基于用户输入对数据库 SELECT 查询执行此操作:

$param = $_GET['username'];

$query=$dbh->prepare("SELECT secret FROM users WHERE username=:param");
$query->bindParam(':param', $param);
$query->execute();

$result = $query -> fetch();

print_r($result);

回答by Stephen

If you want just a single field, you could use fetchColumn instead of fetch - http://www.php.net/manual/en/pdostatement.fetchcolumn.php

如果你只想要一个字段,你可以使用 fetchColumn 而不是 fetch - http://www.php.net/manual/en/pdostatement.fetchcolumn.php

回答by KoolKabin

how about using limit 0,1for mysql optimisation

limit 0,1用于mysql优化怎么样

and about your code:

关于你的代码:

$DBH = new PDO( "connection string goes here" );

$STH - $DBH -> prepare( "select figure from table1" );

$STH -> execute();

$result = $STH ->fetch(PDO::FETCH_ASSOC)

echo $result["figure"];

$DBH = null;

回答by jhloke

Thanks to Steven's suggestion to use fetchColumn, here's my recommendation to cut short one line from your code.

感谢 Steven 建议使用 fetchColumn,这是我的建议,从您的代码中剪短一行。

$DBH = new PDO( "connection string goes here" );
$STH = $DBH->query( "select figure from table1" );
$result = $STH->fetchColumn();
echo $result;
$DBH = null;