我正在尝试使用PHP PDO语法。也许somone可以看看代码的和平。这似乎有点多的努力。
我抓住用户后置输入(ID)。知道我需要计算原因所有用户的BejagFlach值(它们的总和)和ID标识的用户的单值BejagFlach。我这样需要两个查询吗?还是有更短/更好的方式?
/* Fetch POST Data */
$User_Num = $_POST['User_Num'];
/* Build query for one User */
$dbSelect = $objDb->prepare("SELECT BejagFlach,Name FROM Benutzer WHERE lfdNr = :User_Num");
$dbSelect -> setFetchMode(PDO::FETCH_ASSOC);
$dbSelect->execute(array(':User_Num' => $User_Num));
/* Build query for all Users */
$dbSelect2 = $objDb->prepare("SELECT BejagFlach FROM Benutzer");
$dbSelect2 -> setFetchMode(PDO::FETCH_ASSOC);
$dbSelect2->execute();
/* Output + Calculate */
while($row = $dbSelect->fetch()) {
$totalUser += $row['BejagFlach'];
$who = utf8_encode($row['Name']);
}
/* Output + Calculate */
while($row = $dbSelect2->fetch()) {
$totalAll += $row['BejagFlach'];
}
亲切的问候, 托尼
答案 0 :(得分:0)
不,它似乎不太多?
我唯一要改变的是,如果您只是在匹配的记录数量之后,请使用rowCount
<?php
$sel = $dbh->prepare('select * FROM fruit');
$sel->execute();
/* Return number of rows that were deleted */
print("Return number of rows that were selected:\n");
$count = $sel->rowCount();
print("Seleted $count rows.\n");
?>
答案 1 :(得分:0)
由于您至少需要第二个查询,我会这样做,因此您只需要对数据库进行1次请求:
$dbSelect = $objDb->prepare("SELECT BejagFlach,lfdNr FROM Benutzer");
$dbSelect -> setFetchMode(PDO::FETCH_ASSOC);
$dbSelect ->execute();
while($row = $dbSelect->fetch()) {
$totalAll += $row['BejagFlach'];
if( $row['lfdNr'] == $UserNom) {
$totalUser += $row['BejagFlach'];
$who = utf8_encode($row['Name']);
}
}