Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-02-2010, 05:44 AM   PM User | #1
Jon W
Regular Coder

 
Join Date: Jan 2008
Posts: 334
Thanks: 9
Thanked 0 Times in 0 Posts
Jon W is an unknown quantity at this point
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.');
                }
            }
    }
?>
Jon W is offline   Reply With Quote
Old 04-02-2010, 06:27 AM   PM User | #2
PappaJohn
Senior Coder

 
Join Date: Apr 2007
Location: Quakertown PA USA
Posts: 1,028
Thanks: 1
Thanked 125 Times in 123 Posts
PappaJohn will become famous soon enough
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
PappaJohn is offline   Reply With Quote
Old 04-02-2010, 07:12 AM   PM User | #3
Jon W
Regular Coder

 
Join Date: Jan 2008
Posts: 334
Thanks: 9
Thanked 0 Times in 0 Posts
Jon W is an unknown quantity at this point
Hehe... Oops. Thanks. :P
Jon W is offline   Reply With Quote
Old 04-02-2010, 07:45 AM   PM User | #4
kbluhm
Senior Coder

 
kbluhm's Avatar
 
Join Date: Apr 2007
Location: Philadelphia, PA, USA
Posts: 1,502
Thanks: 2
Thanked 258 Times in 254 Posts
kbluhm will become famous soon enough
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.
__________________
ZCE

Last edited by kbluhm; 04-02-2010 at 07:48 AM..
kbluhm is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:27 AM.


Advertisement
Log in to turn off these ads.