PDO查询循环

时间:2018-09-26 18:17:50

标签: php pdo foreach

我有此代码:

$sql = "SELECT id FROM videos";
$stmp = $db->prepare($sql);
$stmp->execute();
while ($row = $stmp->fetchAll(PDO::FETCH_ASSOC)) {

    $vkey = md5($row['id'] . "video");

    $sql = "UPDATE videos SET vkey = :vkey WHERE id = :id";
    $stmp = $db->prepare($sql);
    $stmp->execute(array(
        ":vkey" => $vkey,
        ":id"   => $row['id']
    ));

}

为什么只对第一个选择中的第一个ID执行而不对循环中的所有ID执行?

1 个答案:

答案 0 :(得分:3)

您只需执行以下操作即可完全避免所有这些代码:

UPDATE videos SET vkey = MD5(CONCAT(vkey, 'video'))

(或者您也可以在后端像PHPMyAdmin $sql = "SELECT id FROM videos"; //no reason to use prepare() because you aren't passing variables. $stmp = $db->query($sql); $stmp->execute(); $results = $stmp->fetchAll(PDO::FETCH_ASSOC); //prepare UPDATE query outside of loop, this way you don't send 2 requests to your database for every row $stmp = $db->prepare("UPDATE videos SET vkey = :vkey WHERE id = :id"); foreach($results as $result) { $vkey = md5($result['id']."video"); $stmp->execute( array( ":vkey" => $vkey, ":id" => $result['id'] ) ); } 这样进行查询)


但是,如果出于某种原因要遍历数据库,则可以执行以下操作:

$stmp->rowCount()

此外,通常最好检查循环中的返回值以确保没有错误,您可以使用#include <stdio.h> #include <stdlib.h> #include <string.h> int ticket,tnotran,tran; char pass[40],desti[2],cla,destina[20],clana[20]; float tfare,cfare,nfare,gtnfare; int main(int argc, char *argv[]) { printf("\n\n\t\tTicket NO.: "); scanf("%d",&ticket); while (ticket != 0){ head(); con(); system("pause"); system("cls"); netcomp(); out(); system("pause"); system("cls"); printf("\n\n\t\tTicket NO.: "); scanf("%d",&ticket); } printf("\n\n\t\tTotal No. of Transactions: %d", tran); printf("\n\n\t\tGrand Total of Net Fare: %f", gtnfare); } head(){ printf("\n\t\tPassenger's Name: "); scanf("%s",&pass); printf("\n\t\tDestination Code: "); scanf("%s",&desti); printf("\n\t\tClass Code: "); scanf("%s",&cla); } con(){ if(strcpy(desti, "HK")||strcpy(desti, "hk")){ tfare= 8500; printf("\n\t\tTicket Fare: %.2f", tfare); strcpy(destina,"Hong Kong"); } else if(strcpy(desti, "BK")||strcpy(desti, "bk")){ tfare= 7500; printf("\n\t\tTicket Fare: %.2f", tfare); strcpy(destina,"Bangkok"); } else if(strcpy(desti, "KL")||strcpy(desti, "kl")){ tfare= 7900; printf("\n\t\tTicket Fare: %.2f", tfare); strcpy(destina,"Kualala Lumpur"); } else if(strcpy(desti, "TP")||strcpy(desti, "tp")){ tfare= 8300; printf("\n\t\tTicket Fare: %.2f", tfare); strcpy(destina,"Taipe"); } else{ printf("\n\t\tInvalid Input"); tfare=0; } if(cla== 'E'|| cla=='e'){ cfare= 1500; printf("\n\t\tClass Fare: %.2f", cfare); strcpy(clana,"Economy"); } else if(cla== 'T'|| cla=='t'){ cfare= 2500; printf("\n\t\tClass Fare: %.2f", cfare); strcpy(clana,"Tourist"); } else if(cla== 'F'|| cla=='f'){ cfare= 3500; printf("\n\t\tClass Fare: %.2f", cfare); strcpy(clana,"First Class"); } } netcomp(){ nfare = tfare+ cfare; gtnfare +=nfare; tran += 1; } out(){ printf("\n\n\t\tTicket NO.: %d", ticket); printf("\n\t\tPassenger's Name: %s", pass); printf("\n\t\tDestination: %s", destina); printf("\n\t\tClass: %s", clana); printf("\n\t\tNet Fare: %f", nfare); } 之类的方法来检查是否有任何行受到影响。< / p>