...

View Full Version : PHP OOP Help



srule_
12-24-2007, 07:48 PM
Hello,
Im learning OOP and I am trying to make a Database Connection Class. The following code is not making a successful connection:

connectClass.php:


<?php

class MySqlConnect {

//Variable Declatiation
private $username;
private $password;
private $host;
private $dbname;

//constructure
public function __construct($user, $pass, $host, $database){
$this->username= $user;
$this->password = $pass;
$this->host = $host;
$this->dbname = $database;

//Make the Connection
$connection = @mysql_connect ($host, $user, $pass) OR die ('Could not connect to MySQL: ' . mysql_error() );
if($connection) {
$dp= mysql_select_db($database);
if(!$db) echo "Failed to select $database";
}
else {
echo "Failed to connect to the database";
}
}
}

?>


index.php:


<?php

include('connectClass.php');

$db = new MySqlConnect('root', '', 'localhost', 'cms');

?>


When I run the script I get the error "cannot connect to cms".

Any help would be great!

Inigoesdr
12-24-2007, 07:53 PM
You mixed up the variables:

$dp= mysql_select_db($database); // $dp
if(!$db) echo "Failed to select $database"; // $db

srule_
12-24-2007, 07:59 PM
Ugg, Thank!

I'm allowed making dumb mistake like that because it is the holidays lol :-P



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum