gpt4 book ai didi

MySQL 和 PDO : about efficiency

转载 作者:行者123 更新时间:2023-11-29 08:56:09 26 4
gpt4 key购买 nike

我有以下代码:

<?php
try {
# MySQL with PDO_MYSQL
$DBH = new PDO("mysql:host=*****;dbname=****", "****", "*****");
$DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

# statement handle (prevents injection)
$STH = $DBH->prepare("SELECT Adresse FROM Agences");
$STH->execute();

# statement handle (prevents injection)
$STHNAMES = $DBH->prepare("SELECT `numero-agence` FROM Agences");
$STHNAMES->execute();

$storeArray = Array();
$nameArray = Array();
while ($row = $STH->fetch()) {
$storeArray[] = $row['Adresse'];
}

while ($row = $STHNAMES->fetch()) {
$nameArray[] = $row['numero-agence'];
}

echo json_encode(
Array("theAddress" => $storeArray,
"theName" => $nameArray)
);
}
catch(PDOException $e) {
echo 'There was an issue inserting thing into database: '.$e->getMessage();
}
?>

我的问题是:有没有办法组合这两个查询,并且仍然有一个关联数组以 JSON 编码发送回客户端? (我正在使用 ajax 调用查询这部分 PHP,并且需要结果数据)

谢谢。

最佳答案

可以在同一个查询中完成:

     # statement handle (prevents injection)
$STH = $DBH->prepare("SELECT Adresse, `numero-agence` FROM Agences");
$STH->execute();

$storeArray = Array();
$nameArray = Array();
while ($row = $STH->fetch()) {
$storeArray[] = $row['Adresse'];
$nameArray[] = $row['numero-agence'];
}

关于MySQL 和 PDO : about efficiency,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9923237/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com