【发布时间】:2011-07-24 07:40:00
【问题描述】:
更新 2:
那么这是它可以得到的最优化的吗?
$DBH = new PDO( "connection string goes here" );
$STH = $DBH -> prepare( "select figure from table1" );
$STH -> execute();
$result = $STH -> fetch();
echo $result ["figure"];
$DBH = null;
更新 1:
我知道我可以为 sql 查询添加限制,但我也想摆脱我不应该需要的 foreach 循环。
原始问题:
由于“foreach”部分,我有以下脚本对于从数据库中返回许多行来说是很好的 IMO。
如果我知道我总是只能从数据库中获取 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;
【问题讨论】:
-
$STH - $DBH -> prepare应该是$STH = $DBH -> prepare此页面上的多个位置。 -
可以在break;中使用foreach