PDO不会从mysql返回数据到jQuery AJAX [英] PDO doesnt return data from mysql to jQuery AJAX
问题描述
我正在使用Web应用程序维护餐厅的管理,其中包括以下功能:
I'm working on a web application to maintain the administration of a restaurant, wich the functions such as:
- 在单击一个订单时显示所有订单的列表以及其中的特定数据
- 做出或删除订单
- 维护财务状况
- 等...
首先,我习惯于使用mysqli
扩展名,并且使用了类似这样的功能(带有return
语句):
First i was used to using the mysqli
extension, and was using functions like this (with the return
statement):
function get_all_data_by_order_id($order_id) {
$query = "SELECT customers.first_name,
customers.last_name,
customers.email_adress,
customers.customer_info,
orders.order_info,
orders.total_price,
orders.location,
orders.created
FROM customers
INNER JOIN orders ON customers.id = orders.customer_id
WHERE orders.id = {$order_id}";
return $this->query($query);
}
现在我想在PDO上尝试一下,这样我可以更轻松地使用准备好的语句.
And now i wanted to try it with PDO so i could use prepared statement easier.
我有这个php文件:
try {
$connection = new PDO('mysql:host=localhost;dbname=broodjes-service', 'root', 'password');
$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if (!empty($_GET['order_id'])) {
$order_id = $_GET['order_id'];
$query = "SELECT customers.first_name,
customers.last_name,
customers.email_adress,
customers.customer_info,
orders.order_info,
orders.total_price,
orders.location,
orders.created
FROM customers
INNER JOIN orders ON customers.id = orders.customer_id
WHERE orders.id = :order_id";
$statement = $connection->prepare($query);
$statement->bindParam(":order_id", $order_id, PDO::PARAM_INT);
$statement->execute();
$order_data = $statement->fetch(PDO::FETCH_ASSOC);
$orderObject = array();
$orderObject['header'] = mysqli_fetch_array($order_data);
echo json_encode($orderObject);
$connection = null;
}
} catch (PDOException $e) {
echo $e->getMessage();
die();
}
此段javascript会调用它,以使其看起来好像页面未在重新加载:
Wich is getting called by this piece of javascript to make it look like the page is'nt reloading:
function select_order(order) {
var item = $(order);
if (!item.hasClass("selectedRow")) {
if (!selectedOrderInformation.is(":visible")) {
switchScreen(selectedOrderInformation, financeOverview);
}
item.parent().find(".selectedRow").removeClass("selectedRow");
item.addClass("selectedRow");
selectedOrderInformation.html("loading......");
$.ajax({
url: "includes/functions/select-order.php",
type: "get",
dataType: 'json',
data: {order_id: item.attr("data-order-index")},
success: function (data) {
selectedOrderInformation.html('<h3>' + data['header']['first_name'] + '</h3>'); }
});
} else {
console.log("DEBUG: Row is already selected");
}
}
问题
当我尝试在列表中选择一个订单时,什么都没有发生.屏幕停留在正在加载..."屏幕上.似乎甚至没有执行查询.
When i try to select a order inside the list theyre in, nothing is happening. The screen stays on the 'loading...' screen. It seems like it is'nt even executing the query.
问题
我该如何做到这一点,以便我的javascript文件通过从mysql数据库中部署数据来对php文件做出实际反应?
How can i make it so that my javascript file actually reacts on the php file by deploying data from the mysql database?
推荐答案
请勿混合PDO和MySQLi
由于您只想选择一行,因此请使用 fetch()
或 fetchAll()[0]
推荐方式:
$statement->execute();
$orderObject = array();
$orderObject['header'] = $statement->fetch(); //or: $statement->fetchAll()[0]
echo json_encode($orderObject);
这篇关于PDO不会从mysql返回数据到jQuery AJAX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!