class DatabaseConnection{
private $connection;
private $username;
private $password;
private $serverName;
private $database;
private $database_found = false;
//constructor with params
public function __construct($database,$username,$password,$serverName){
$this->$username = $username;
$this->$password = $password;
$this->$serverName = $serverName;
$this->$database = $database;
}
//estabilish database connection
public function connect()
{
$connection = mysql_connect($this->serverName,$this->username,$this->password);
if($connection)
{
echo "Connection was successful!\n";
$db = mysql_select_db($this->database,$connection);
if($db)
{
echo "Database found!\n";
$this->database_found = true;
}
else{
echo "Database not found!\n";
}
}
else {
echo "Failed to connect to the database!\n";
}
}
//closes connection
public function close_connection(){
mysql_close();
echo "Connection closed!\n";
}
public function getConnection(){
return $this->connection;
}
public function getDatabaseState(){
return $this->database_found;
}
}
This is my class for database connection. It connects to mysql, but for some reason it doesn't want to connect to the database (I've tried to insert database name as a string in mysql_select_database
but still does not work). Database exist cause It was manually created. Thanks for the help.