webdevdea Posted February 8, 2014 Share Posted February 8, 2014 Hello I need a little assistance with my code please its saying class db not found here is a link http://dandewebwonders.com/HeadFirst/Library.php Here is my code <?php //if we got something through $_POST if (isset($_POST['search'])) { // here you would normally include some database connection include('db.php'); $db = new db($DB_HOST, $DB_USER, $DB_PASSWORD, $DB_NAME); // never trust what user wrote! We must ALWAYS sanitize user input $word = mysql_real_escape_string($_POST['search']); $word = htmlentities($word); // build your search query to the database $sql = "SELECT title, download FROM test WHERE content LIKE '%" . $word . "%' ORDER BY title LIMIT 10"; // get results $row = $db->select_list($sql); if(count($row)) { $end_result = ''; foreach($row as $r) { $result = $r['title']; // we will use this to bold the search word in result $bold = '<span class="found">' . $word . '</span>'; $end_result .= '<li>' . str_ireplace($word, $bold, $result) . '</li>'; } echo $end_result; } else { echo '<li>No results found</li>'; } } ?> Thank you so much in advance, Im trying to make a searchable library for my pdf books I have in various cloud accounts. Quote Link to comment https://forums.phpfreaks.com/topic/286035-class-db-not-found/ Share on other sites More sharing options...
denno020 Posted February 8, 2014 Share Posted February 8, 2014 The class db() on this line $db = new db($DB_HOST, $DB_USER, $DB_PASSWORD, $DB_NAME); isn't a standard PHP one. You need to include the PHP file that has the definition for that class. Denno Quote Link to comment https://forums.phpfreaks.com/topic/286035-class-db-not-found/#findComment-1468161 Share on other sites More sharing options...
mac_gyver Posted February 8, 2014 Share Posted February 8, 2014 there's nothing obviously wrong with your code (outside of using a mysql function instead of your database class to escape string data), provided your class is defined in db.php and the include statement for that file is working. what have you done to troubleshoot the problem to narrow down the possibilities? Quote Link to comment https://forums.phpfreaks.com/topic/286035-class-db-not-found/#findComment-1468162 Share on other sites More sharing options...
webdevdea Posted February 8, 2014 Author Share Posted February 8, 2014 This is my db.php file class new { private $DB_HOST = "localhost"; private $DB_USER = "stuff"; private $DB_PASSWORD = "stuff"; private $DB_NAME = "stuff"; function __construct() { $connection = mysql_connect($this->DB_HOST, $this->DB_USER, $this->DB_PASSWORD) or die("Could not connect to the database:<br />" . mysql_error()); mysql_select_db($this->DB_NAME, $connection) or die("Database error:<br />" . mysql_error()); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/286035-class-db-not-found/#findComment-1468176 Share on other sites More sharing options...
webdevdea Posted February 8, 2014 Author Share Posted February 8, 2014 I am running the code and searching the errors, trying to figure out what is wrong, this is my do_search.php this is the error I am getting Call to undefined method db::select_list() in /home3/aundie/public_html/dandewebwonders.com/HeadFirst/do_search.php on line 17 <?php //if we got something through $_POST if (isset($_POST['search'])) { // here you would normally include some database connection include('db.php'); $db = new db($DB_HOST, $DB_USER, $DB_PASSWORD, $DB_NAME); mysql_connect("localhost", "aundie_test", "pass_word") or die(mysql_error()); mysql_select_db("aundie_books") or die(mysql_error()); // never trust what user wrote! We must ALWAYS sanitize user input $word = mysql_real_escape_string($_POST['search']); $word = htmlentities($word); // build your search query to the database $sql = "SELECT title, download FROM test WHERE content LIKE '%" . $word . "%' ORDER BY title LIMIT 10"; // get results $row = $db->select_list($sql); if(count($row)) { $end_result = ''; foreach($row as $r) { $result = $r['title']; // we will use this to bold the search word in result $bold = '<span class="found">' . $word . '</span>'; $end_result .= '<li>' . str_ireplace($word, $bold, $result) . '</li>'; } echo $end_result; } else { echo '<li>No results found</li>'; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/286035-class-db-not-found/#findComment-1468177 Share on other sites More sharing options...
mac_gyver Posted February 8, 2014 Share Posted February 8, 2014 This is my db.php file class new { private $DB_HOST = "localhost"; .... the name of your class in that file is not db. you named your class new. Quote Link to comment https://forums.phpfreaks.com/topic/286035-class-db-not-found/#findComment-1468179 Share on other sites More sharing options...
webdevdea Posted February 8, 2014 Author Share Posted February 8, 2014 the name of your class in that file is not db. you named your class new. I do not understand. am i not to name the class new? I am so confused <?php class new db { private $DB_HOST = "localhost"; private $DB_USER = "aundie_test"; private $DB_PASSWORD = "pass_word"; private $DB_NAME = "aundie_books"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/286035-class-db-not-found/#findComment-1468190 Share on other sites More sharing options...
mac_gyver Posted February 8, 2014 Share Posted February 8, 2014 i recommend that you read the documentation - http://us3.php.net/manual/en/language.oop5.basic.php the very first sentence describes the syntax for a class definition. Quote Link to comment https://forums.phpfreaks.com/topic/286035-class-db-not-found/#findComment-1468194 Share on other sites More sharing options...
Solution KevinM1 Posted February 9, 2014 Solution Share Posted February 9, 2014 (edited) I do believe that "new" is a reserved keyword in PHP, so you're not supposed to name anything that. More to the point, the proper way to instantiate an object is to write something along the lines of: $obj = new <class name>(/* argument list */);Where <class name> is what you choose when you write the class definition: class <class name>{ // class definition}If you want new DB to work you need to rename your class "DB". Edited February 9, 2014 by KevinM1 Quote Link to comment https://forums.phpfreaks.com/topic/286035-class-db-not-found/#findComment-1468236 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.