Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在jQuery.Post方法中执行mySQL查询_Php_Mysql_Zend Framework2 - Fatal编程技术网

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

Php 在jQuery.Post方法中执行mySQL查询,php,mysql,zend-framework2,Php,Mysql,Zend Framework2,我创建了一个javascript文件,其中包含一个js,用于在更改下拉列表时触发事件。报告如下: // /public/js/custom.js jQuery(function($) { $("#parent").change(function(event){ event.preventDefault(); var parentID = $('#parent').val(); var id = $('#id').val();

我创建了一个javascript文件,其中包含一个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");
    });
});
在MenuTable.php中有一个方法GetPositionsByID,如下所示:

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;
}
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();
}
然后什么也没发生。如果替换GetPositionsByID方法中的所有代码,并返回如下数组:

return array('1' => 'one', '2' => 'two');
它工作得很好,但是我想从数据库中获取数据。有人知道为什么执行会在这一行失败吗

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

提前感谢

问题是适配器为空。

首先:为什么在PHP上下文中使用
console.log()
?这一定是输入错误,因为我敢打赌这会产生语法错误(变量名前面没有$)。第二:您是否尝试过在自己的数据库上运行SQL,以查看是否确实在选择某些内容?很可能是查询没有返回任何行。该查询确实有效,我尝试了该查询,但它确实返回了结果。我添加了try-catch块以查看是否存在异常。在我跨过$statement=$this->adapter->query($sql)之后,没有抛出异常;行,它不会转到下一行。这里要学习的教训是在开发过程中打开所有错误。编辑php.ini并找到
错误报告的行
,然后将其设置为
E|u ALL | E_STRICT
。然后确保
display\u errors
处于启用状态。
$statement =  $this->adapter->query($sql);