调用成员函数ERROR PHP

调用成员函数ERROR PHP,php,jquery,mysql,ajax,jquery-select2,Php,Jquery,Mysql,Ajax,Jquery Select2,我试图使用本教程使用MySQL数据库中的数据填充Select2下拉列表: 这是我正在使用的PHP脚本(从原始脚本稍作修改): 这就是我收到的错误: [08-Feb-2014 18:28:06太平洋/奥克兰]PHP通知:未定义 变量:db in/Users/../app/scripts/serviceplan_values.php on line 6[08-Feb-2014 18:28:06太平洋/奥克兰]PHP致命错误:调用 中非对象上的成员函数prepare() /第6行的Users/../a

我试图使用本教程使用MySQL数据库中的数据填充Select2下拉列表:

这是我正在使用的PHP脚本(从原始脚本稍作修改):

这就是我收到的错误:

[08-Feb-2014 18:28:06太平洋/奥克兰]PHP通知:未定义 变量:db in/Users/../app/scripts/serviceplan_values.php on line 6[08-Feb-2014 18:28:06太平洋/奥克兰]PHP致命错误:调用 中非对象上的成员函数prepare() /第6行的Users/../app/scripts/serviceplan_values.php


有什么想法吗?

错误很可能出现在
connect.php
中,您需要在其中设置
$db
变量。

在非对象上调用成员函数的
可能重复
通常意味着您认为
$db
变量已正确设置,但实际上只是
null
。做一个
var_dump($db)
来验证这是有道理的,图坦卡蒙说他不会涉及这个,你认为我需要把它设置成什么?你认为你能帮上忙吗?编辑你的问题,包括你的
connect.php
(确保先删除密码和其他敏感信息!);那我希望我能帮上忙。
<?php
    // setup databse connection
    require("../scripts/connect.php");

    // i have set the limit to 40 to speed up results
    $result = $db->prepare("SELECT id,model FROM vehicles WHERE model LIKE :term ORDER BY model ASC LIMIT 0,40");

    // bind the value for security with the wildcard % attached.
    $result->bindvalue(':term','%'.$_GET["q"].'%',PDO::PARAM_STR);
    $result->execute();

    // make sure there are some results else a null query will be returned
    if($result->rowcount() != 0) {
        while($row = $result->fetch(PDO::FETCH_ASSOC)){
            $answer[] = array("id"=>$row['id'],"text"=>$row['id']." - ".$row['model']);
            // the text I want to show is in the form of option id - option
        }
    } else {
    // 0 results send a message back to say so.
        $answer[] = array("id"=>"0","text"=>"No Results Found..");
    }

    // finally encode the answer to json and send back the result.
    echo json_encode($answer);
CREATE TABLE `vehicles` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `carline` varchar(125) NOT NULL,
    `model` varchar(255) NOT NULL,
    `year` varchar(12) NOT NULL,
    `engine` varchar(255) NOT NULL,
    `airconditioning` enum('Yes','No') NOT NULL,
    `transmission` enum('Automatic','Manual') NOT NULL,
    `drive` enum('2WD','4WD') NOT NULL,
    `designation` enum('Passenger','Commercial') NOT NULL,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=14 ;