我正在尝试使用本教程使用MySQL数据库中的数据填充Select2下拉列表:http://www.southcoastweb.co.uk/jquery-select2-ajax-tutorial/
这是我正在使用的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 ;
这是我收到的错误:
[08-Feb-2014 18:28:06太平洋/奥克兰] PHP注意:未定义 变量:db在/Users/.../app/scripts/serviceplan_values.php上 6 [08-Feb-2014 18:28:06太平洋/奥克兰] PHP致命错误:致电a 成员函数prepare()在非对象中 第6行/Users/.../app/scripts/serviceplan_values.php
任何想法?
答案 0 :(得分:0)
错误最有可能发生在connect.php
,您需要设置$db
变量。