Jump to content

Class 'db' not found


webdevdea
Go to solution Solved by KevinM1,

Recommended Posts

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.

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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());
    }
}
 
?>
Link to comment
Share on other sites

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>';
    }
}
?>

Link to comment
Share on other sites

 

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";

 
}
 
?>

Link to comment
Share on other sites

  • Solution

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 by KevinM1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.