【问题标题】:Call to a member function ERROR PHP调用成员函数 ERROR PHP
【发布时间】: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


【解决方案1】:

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

【讨论】:

  • 啊哈说得有道理,tut说他不会覆盖,你认为我需要将它设置为什么?
  • 你觉得你能帮忙吗?
  • 编辑您的问题以包含您的connect.php确保先删除密码和其他敏感信息!);那我希望我能帮上忙。
猜你喜欢
  • 1970-01-01
  • 2011-11-29
  • 2017-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多