View Single Post
Old Dec 10th, 2006, 10:22 PM   #1
brokenhope
Hobbyist Programmer
 
Join Date: Apr 2005
Posts: 126
Rep Power: 4 brokenhope is on a distinguished road
Am I on the right track with OOP?

I havent programmed with PHP in over a year and Im starting again, by jumping into OOP, something I never really used before... kinda confused me at first, working on a mysql class. Anyways, heres what I have for the connection, is this complete sloppyness? or acceptable?

testmysql.php
<?php
require_once "config.php";
require_once "classes/mysql.php";

$db = new mysqldb;
$db->dbhost = $dbhost;
$db->dbusername = $dbusername;
$db->dbpassword = $dbpassword;
$db->dbname = $dbname;

$conn = $db->connect();

if (!$conn) {
	echo 'Error Connecting to the database.';
}
else {
	echo 'Successful Connection.';
}
?>
(config.php contains $dbhost, username pass and name variables)

mysql.php
<?php
class mysqldb
{
var $dbhost;
var $dbusername;
var $dbpassword;
var $dbname;

	function connect() {
		$dblink = mysql_connect($this->dbhost, $this->dbusername, $this->dbpassword);
			if (!$dblink) {
			}
			elseif (!mysql_select_db($this->dbname, $dblink)) {
				$dblink = false;
			}
		return $dblink;
	}
}
?>

I wrote the connect to select the database too because my scripts only using 1 database and it would be quite an annoyance to have to have an extra line in every file to select the database. How does it look?
brokenhope is offline   Reply With Quote