post网页查询mysql,在jQuery.Post方法中执行mySQL查询

博客内容涉及一个JavaScript事件监听器,用于在下拉列表更改时触发AJAX请求。请求发送到PHP控制器的GetMenuChildrenAction方法,该方法尝试从数据库获取数据。问题出在数据库查询上,由于适配器为null,导致查询失败。当用静态数据替换数据库查询时,代码工作正常。解决方案是确保数据库适配器已正确初始化。
摘要由CSDN通过智能技术生成

I have created a javascript file that contains js to trigger an event onchange of a drop down list. The JS is below:

// /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($("")

.attr("value", value).text(key));

});

} else {

// print error message

alert("something went wrong in Post");

}

}, 'json');

alert("After Post");

});

});

In my Controller.php I have an function GetMenuChildrenAction as shown below:

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;

}

In the MenuTable.php there is a Method GetPositionsByID as shown below:

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();

}

It all seems to be hooked up correctly, when I debug the code, I get the this line:

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

and then nothing happens. If I replace all the code in the GetPositionsByID method, and simply return an array like the following:

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

it works great, however i want to get the data from the DB. Does anyone know why the execute would fail on this line?

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

Thanks in advance

解决方案

The issue was that the adapter was null.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值