...

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



Jon W
04-02-2010, 06:44 AM
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
$host = "localhost";
$user = "user";
$pass = "password";
$db = "mydatabase";
?>


common.php


<?php
require("conn.php");
require("db.php");
$db = new db();

$db->conn($host, $user, $pass, $db);
?>


db.php whcih is the class file.


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

PappaJohn
04-02-2010, 07:27 AM
$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.

Jon W
04-02-2010, 08:12 AM
Hehe... Oops. Thanks. :P

kbluhm
04-02-2010, 08:45 AM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum