Class connection

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
webgroundz
Forum Commoner
Posts: 58
Joined: Thu Jun 21, 2007 1:20 am
Location: Philippines

Class connection

Post 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;
?>
Last edited by webgroundz on Mon Jun 25, 2007 1:52 am, edited 4 times in total.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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.
(#10850)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Moved to PHP-Code.

Code Critique is not a troubleshooting forum.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

You may also want to check if the result is actually a valid MySQL resource.

Code: Select all

gettype($result);
djot
Forum Contributor
Posts: 313
Joined: Wed Jan 14, 2004 10:21 am
Location: planet earth
Contact:

Post 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
-
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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:
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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. ^_^
User avatar
webgroundz
Forum Commoner
Posts: 58
Joined: Thu Jun 21, 2007 1:20 am
Location: Philippines

Post 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:
Post Reply