UPDATE 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;
UPDATE 1:
我知道我可以向SQL查询添加限制,但我也想消除foreach循环,这是我不需要的.
ORIGINAL QUESTION:
我有下面的脚本,由于"foreach"部分,它很适合从数据库返回许多行.
如果我知道我总是只能从数据库中得到一行,我该如何优化它呢.如果我知道我只能从数据库中得到一行,我不明白为什么我需要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;