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;
}
}
这是我的数据库连接类。它连接到MySQL,但由于某种原因,它不想连接到数据库(我试图插入数据库名称作为字符串在mysql_select_database
,但仍然无法正常工作)。数据库存在的原因这是手动创建的。谢谢您的帮助。通过php连接到mysql
谢谢。我会看看它! – Chris 2015-02-08 11:12:53
@Chris刚编辑添加更多细节 – tleb 2015-02-08 11:25:28
我知道OOP的实践:)刚刚使用Java,但对于这个项目对我来说更好PHP – Chris 2015-02-08 16:04:52