Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Jan 2008
    Posts
    334
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Catchable fatal error: Object of class db could not be converted to string

    Hello all. I ran into a error that I can't seem to figure out. This error is created from the mysql_select_db() function. All looks like it should work fine to me. So while I don't understand this maybe someone here will.

    conn.php
    PHP Code:
    <?php
        $host 
    "localhost";
        
    $user "user";
        
    $pass "password";
        
    $db "mydatabase";
    ?>
    common.php
    PHP Code:
    <?php
        
    require("conn.php");
        require(
    "db.php");
        
    $db = new db();
        
        
    $db->conn($host$user$pass$db);
    ?>
    db.php whcih is the class file.
    PHP Code:
    <?php
        
    class db {
                public function 
    conn($host$user$pass$db) {
                    
    $con mysql_connect($host$user$pass);
                    if(!
    $con) {
                        die(
    'Database Error. Failed to connect.');
                    }
                    
                    
    $select_db mysql_select_db($db$con);
                    if(!
    $select_db) {
                        die(
    'Unable to select database.');
                    }
                }
        }
    ?>

  • #2
    Senior Coder
    Join Date
    Apr 2007
    Location
    Quakertown PA USA
    Posts
    1,028
    Thanks
    1
    Thanked 125 Times in 123 Posts
    Code:
    $db = new db();
        
    $db->conn($host, $user, $pass, $db);
    You've named both the instance of your DB object, and a string variable holding the name of the database as $db. You'll need to change one of them.

    What that error means is that the $db->conn function is expecting a string as the database name, but you are attempting to pass an object.
    John

  • #3
    Regular Coder
    Join Date
    Jan 2008
    Posts
    334
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Hehe... Oops. Thanks. :P

  • #4
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Quote Originally Posted by PappaJohn View Post
    What that error means is that the $db->conn function is expecting a string as the database name, but you are attempting to pass an object.
    That's not quite correct. It means you are attempting to convert an object to a string, but the object has no way of handling it. It has no __toString() magic method.
    Last edited by kbluhm; 04-02-2010 at 08:48 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •