【发布时间】:2014-02-08 05:52:26
【问题描述】:
我正在尝试使用本教程使用 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 Pacific/Auckland] PHP 通知:未定义 变量:db in /Users/.../app/scripts/serviceplan_values.php 上线 6 [08-Feb-2014 18:28:06 Pacific/Auckland] PHP 致命错误:调用 非对象上的成员函数 prepare() /Users/.../app/scripts/serviceplan_values.php 在第 6 行
有什么想法吗?
【问题讨论】:
-
call to a member function on a non object通常意味着您认为$db变量已正确设置,但实际上它只是null。做一个var_dump($db)来验证
标签: php jquery mysql ajax jquery-select2