【发布时间】:2015-10-29 15:24:02
【问题描述】:
我正在编写依赖于从 MySQL 数据库返回的数据的 angularjs 应用程序。
Angularjs 代码发出 HTTP 请求并将返回的数据记录到控制台:
$http({method: 'POST', url: 'php/return_something.php', headers: {'Content-Type': 'application/json'}})
.success(function(data, status, headers, config) {
//The API call to the back-end was successful (i.e. a valid session)
console.log(data);
})
.error(function(data, status, headers, config) {
console.log("Error.");
});
return_something.php
try
{
$conn = new PDO("mysql:host=".DB_HOST."; dbname=".DB_DATABASE, DB_USER, DB_PASSWORD);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = $conn->prepare("SELECT count(id_table_x) from table_x; SELECT count(id_table_y) from table_y;");
$sql->execute();
// set the resulting array to associative
$results = $sql->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($results);
}
catch(PDOException $e)
{
echo $sql . "<br />" . $e->getMessage();
}
$conn = null;
在控制台中记录了一个包含一个元素的数组。
[对象]
在这个对象内部有第一个查询的结果(“SELECT count(id_table_x) from table_x”)。
我应该改变什么才能看到第二个查询的结果(“SELECT count(id_table_y) from table_y”)作为这个数组的第二个元素?任何帮助表示赞赏。
【问题讨论】:
-
AFAIK PDO 不支持在单个准备好的语句中进行多个查询。
标签: php mysql angularjs http select