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
webgroundz
Forum Commoner
Posts: 58 Joined: Thu Jun 21, 2007 1:20 am
Location: Philippines
Post
by webgroundz » Mon Jun 25, 2007 12:25 am
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.
Christopher
Site Administrator
Posts: 13596 Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US
Post
by Christopher » Mon Jun 25, 2007 1:09 am
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)
John Cartwright
Site Admin
Posts: 11470 Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:
Post
by John Cartwright » Mon Jun 25, 2007 1:16 am
Moved to PHP-Code.
Code Critique is not a troubleshooting forum.
superdezign
DevNet Master
Posts: 4135 Joined: Sat Jan 20, 2007 11:06 pm
Post
by superdezign » Mon Jun 25, 2007 5:18 am
You may also want to check if the result is actually a valid MySQL resource.
djot
Forum Contributor
Posts: 313 Joined: Wed Jan 14, 2004 10:21 am
Location: planet earth
Contact:
Post
by djot » Mon Jun 25, 2007 5:36 am
-
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
-
John Cartwright
Site Admin
Posts: 11470 Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:
Post
by John Cartwright » Mon Jun 25, 2007 5:39 am
superdezign wrote: You may also want to check if the result is actually a valid MySQL resource.
is_resource()
superdezign
DevNet Master
Posts: 4135 Joined: Sat Jan 20, 2007 11:06 pm
Post
by superdezign » Mon Jun 25, 2007 5:43 am
Jcart wrote: superdezign wrote: You may also want to check if the result is actually a valid MySQL resource.
is_resource()
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. ^_^
webgroundz
Forum Commoner
Posts: 58 Joined: Thu Jun 21, 2007 1:20 am
Location: Philippines
Post
by webgroundz » Mon Jun 25, 2007 5:09 pm
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..