Page 1 of 1

Class connection

Posted: Mon Jun 25, 2007 12:25 am
by webgroundz
I want to create a class for connecting to database
i am trying to connect in two databases:
but it has an error:
Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in C:\web\htdocs\websites\angel_test\angel.php on line 70

what's wrong with my code?.
anyone can help.
thanks.

Code: Select all

<?php
class DBConnection
{
	public $db;
	public $username;
	public $password;
	public $host;
	
	/**
	 * Constructor
	 *
	 * @param string $username
	 * @param string $password
	 * @param string $host
	 */
	public function __construct($host, $username, $password)
	{
		$this->host = $host;	
		$this->username = $username;
		$this->password = $password;

	}

	/**
	 * Connect to database if database exist
	 *
	 * @return string connection
	 */
	public function connectToDatabase()
	{
	
		$this->connect = @mysql_connect($this->host, $this->username, $this->password)or 
		trigger_error("Unexpected connection, try setting details first.", E_USER_ERROR);
		return $this->connect;
	}
	
	/**
	 * Select database persistent
	 *
	 * @param string $db
	 * @return database
	 */
	public function selectDatabase($db)
	{
		return mysql_select_db($db, $this->connect)or die('could not select db');
	}
	
	/**
	 * Close connection
	 *
	 * @return unset connection
	 */
	public function closeConnection()
	{
		return mysql_close($this->connect);		
	}
}
//usage

$db1 = new DBConnection("localhost", "root", "");
//connection 1
$con1 = $db1->connectToDatabase();
$d1 = $db1->selectDatabase("sample1");
//connection 2
$con2 = $db1->connectToDatabase();
$d2 = $db1->selectDatabase("sample2");

$result = mysql_query("select * from tbl_sample1",$con1);
$row = mysql_fetch_object($result);
echo $row->address;
?>

Posted: Mon Jun 25, 2007 1:09 am
by Christopher
That's an excellent start. If you search these forums you should be able to find some recent thread about DB Connection classes that you will find helpful. They add a query() method to the Connection class that returns a Result object.

Posted: Mon Jun 25, 2007 1:16 am
by John Cartwright
Moved to PHP-Code.

Code Critique is not a troubleshooting forum.

Posted: Mon Jun 25, 2007 5:18 am
by superdezign
You may also want to check if the result is actually a valid MySQL resource.

Code: Select all

gettype($result);

Posted: Mon Jun 25, 2007 5:36 am
by djot
-
Using classes is a good idea, while this in your code is not

Code: Select all

$result = mysql_query("select * from tbl_sample1",$con1);
$row = mysql_fetch_object($result);
echo $row->address;
djot
-

Posted: Mon Jun 25, 2007 5:39 am
by John Cartwright
superdezign wrote:You may also want to check if the result is actually a valid MySQL resource.

Code: Select all

gettype($result);
is_resource() :wink:

Posted: Mon Jun 25, 2007 5:43 am
by superdezign
Jcart wrote:
superdezign wrote:You may also want to check if the result is actually a valid MySQL resource.

Code: Select all

gettype($result);
is_resource() :wink:
I was thinking along the lines of finding out what it is so he could fix it. However, after actually reading the code, all it really could be is either a resource or an error. So yeah.. screw gettype(). I was just excited to have learned it yesterday. ^_^

Posted: Mon Jun 25, 2007 5:09 pm
by webgroundz
i am trying to connect and querying also in two databases, is that possible?
what is wrong with my code?

thanks for all your suggestion.. :wink: