Title: Call to a member function ERROR PHP
Tags: php;jquery;mysql;ajax;jquery-select2
Question: I am trying to use this tutorial to populate a Select2 drop down with data from a MySQL Database: http://www.southcoastweb.co.uk/jquery-select2-ajax-tutorial/
This is the PHP script I am using (Slightly modified from original):
<?php
// setup databse connection
require("../scripts/connect.php");
// i have set the limit to 40 to speed up results
$result = $db-&gt;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-&gt;bindvalue(':term','%'.$_GET["q"].'%',PDO75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0PARAM_STR);
$result-&gt;execute();
// make sure there are some results else a null query will be returned
if($result-&gt;rowcount() != 0) {
while($row = $result-&gt;fetch(PDO75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0TCH_ASSOC)){
$answer[] = array("id"=&gt;$row['id'],"text"=&gt;$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"=&gt;"0","text"=&gt;"No Results Found..");
// finally encode the answer to json and send back the result.
echo json_encode($answer);
This is the DB layout:
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 ;
And this is the Error I am recieving:
[08-Feb-2014 18:28:06 Pacific/Auckland] PHP Notice: Undefined
variable: db in /Users/.../app/scripts/serviceplan_values.php on line
6 [08-Feb-2014 18:28:06 Pacific/Auckland] PHP Fatal error: Call to a
member function prepare() on a non-object in
/Users/.../app/scripts/serviceplan_values.php on line 6
Any Ideas?
Comment: possible duplicate of [500 (Internal Server Error) jquery.js:8706](http://stackoverflow.com/questions/21642097/500-internal-server-error-jquery-js8706)

Comment: `call to a member function on a non object` typically means for you that you think the `$db` variable has been properly set, but its really just `null`. do a `var_dump($db)` to verify

Here is the accepted answer: The error is most probably in connect.php, where you need to set the $db variable.
Comment for this answer: Edit your question to include your `connect.php` (**make sure to delete the password and other sensitive information first!**); then I hope I can help.
Comment for this answer: Ah hah that makes sense, the tut said he wouldn't cover that, what do you think I would need to set it as?
Comment for this answer: Do you think you could help?


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 2, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2014:00:02/08/14', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<php><jquery><mysql><ajax><jquery-select2>', 'Title': 'Call to a member function ERROR PHP', 'ViewCount': 247, 'id': 21642512, 'user_AboutMe': '<p>Front end developer, Workflow enthusiast, Recreational fisherman.</p>\n', 'user_AccountId': 2185639.0, 'user_DisplayName': 'Otis Wright', 'user_Reputation': 1840.0, 'user_Views': 161.0, 'user_id': 1934115.0}}