gpt4 book ai didi

php - 在 jQuery.Post 方法中执行 mySQL 查询

转载 作者:行者123 更新时间:2023-11-30 23:19:12 26 4
gpt4 key购买 nike

我创建了一个包含 js 的 javascript 文件来触发下拉列表的 onchange 事件。 JS如下:

// /public/js/custom.js

jQuery(function($) {
$("#parent").change(function(event){
event.preventDefault();
var parentID = $('#parent').val();
var id = $('#id').val();
$.post("/menu/GetMenuChildren", {pID: parentID, thisID: id },
function(data){
if(data.response === true){
var $el = $("#Position");
$el.empty(); // remove old options
$.each(data.newOptions, function(key, value) {
$el.append($("<option></option>")
.attr("value", value).text(key));
});
} else {
// print error message
alert("something went wrong in Post");
}
}, 'json');

alert("After Post");
});
});

在我的 Controller.php 中,我有一个函数 GetMenuChildrenAction,如下所示:

public function GetMenuChildrenAction()
{
$request = $this->getRequest();
$response = $this->getResponse();
if ($request->isPost())
{
$post_data = $request->getPost();
$parent_id = $post_data['pID'];
$id = $post_data['thisID'];
//$this->form->get('Position')->SetOptionValues(
$newOptions = $this->getMenuTable()->GetPositionsByID($parent_id, $id);
if(isset($newOptions))
{
$response->setContent(\Zend\Json\Json::encode(array('response' => true, 'newOptions' => $newOptions)));
}
else
{
$response->setContent(\Zend\Json\Json::encode(array('response' => false)));
}
}
return $response;
}

在 MenuTable.php 中有一个方法 GetPositionsByID,如下所示:

public function GetPositionsByID($parentID, $id)
{
if($parentID == 0)
{
$menu = $this->getMenu($this->id);
$parentID = $menu->parent_id;
}
if(isset($parentID))
{
$sql = "Select ID,Label from Menu Where parent_ID = " . $parentID . " and id > 1 and id <> " . $id . " Order by Position,Label";

try
{
$statement = $this->adapter->query($sql);
}
catch(Exception $e) {
console.log('Caught exception: ', $e->getMessage(), "\n");
}

$res = $statement->execute();

$rows = array();
$i = 0;
foreach ($res as $row) {
$i = $i + 1;
$rows[$i] = array (
$i => $row['Label']
);
}

return $rows;
}
return array();
}

一切似乎都连接正确,当我调试代码时,我得到了这一行:

$statement =  $this->adapter->query($sql);

然后什么也没有发生。如果我替换 GetPositionsByID 方法中的所有代码,并简单地返回一个数组,如下所示:

return array('1' => 'one', '2' => 'two');

效果很好,但是我想从数据库中获取数据。有谁知道为什么执行会在此行失败?

$statement =  $this->adapter->query($sql);

提前致谢

最佳答案

问题是适配器为空。

关于php - 在 jQuery.Post 方法中执行 mySQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16310031/

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