i am trying to connect to database in php , Database name is photo_gallery
following is my code
Database.php
define("DB_SERVER", "localhost");
define("SERVER_NAME", "root");
define("DB_PASS" ,"");
define("DB_NAME", "photo_gallery");
class MySQLDatabase{
function __construct(){
$this->open_connection();
}
private $connection ;
public function open_connection (){
$this->connection = mysqli_connect(DB_SERVER, DB_NAME, DB_PASS);
if(!$this->connection){
die("Connection failed".mysqli_error());
}
else {
$db_select= mysqli_select_db($this->connection,DB_NAME);
if(!$db_select){
echo 'Database selection failed '.mysqli_error($this->connection);
}
}
}
public function query($sql){
$query = mysqli_query($this->connection,$sql);
$this->confirm_query($query);
return $query;
}
private function confirm_query($result_set){
$query= mysqli_query($this->connection,$result_set);
if(!$query){
die('Database query failed '.mysqli_error());
}
}
public function close_connection(){
if(isset($this->connection)){
mysqli_close($this->connection);
unset($this->connection);
}
}
}
now when i run i get following error
Database selection failed Access denied for user: ' @' @ localhost ' . Base ' photo_gallery
i have confirmed username / password and db details but still unable to resolve . Please guide me about it
You haven't supplied a username - your mysqli_connect(DB_SERVER, DB_NAME, DB_PASS)
line doesn't define a user, so you're trying to connect to your database with no user.
Try mysqli_connect(DB_SERVER, SERVER_NAME, DB_NAME, DB_PASS)
instead.
second parameter of the connect method of mysqli-connect is the USERNAME . use it like this
$this->connection = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASS, DB_NAME );
Your problem is that you chose bad names for your constants..
SERVER_NAME is not a good name for the database username..
As a consecuence of this, you used it wrong.. I suggest to change the constant to DB_USER and use it properly in the mysqli_connect() function..
Remember to use names that really represent what they do/refer..
This is the syntax:
$conn= mysqli_connect("myhost","myuser","mypassw","mydb");
Here is the code:
Database.php
define("DB_SERVER", "localhost");
define("SERVER_NAME", "root");
define("DB_PASS" ,"");
define("DB_NAME", "photo_gallery");
class MySQLDatabase{
function __construct(){
$this->open_connection();
}
private $connection ;
public function open_connection (){
$this->connection = mysqli_connect(DB_SERVER, SERVER_NAME, DB_PASS, DB_NAME);
if(!$this->connection){
die("Connection failed".mysqli_error());
}
else {
$db_select= mysqli_select_db($this->connection,DB_NAME);
if(!$db_select){
echo 'Database selection failed '.mysqli_error($this->connection);
}
}
}
public function query($sql){
$query = mysqli_query($this->connection,$sql);
$this->confirm_query($query);
return $query;
}
private function confirm_query($result_set){
$query= mysqli_query($this->connection,$result_set);
if(!$query){
die('Database query failed '.mysqli_error());
}
}
public function close_connection(){
if(isset($this->connection)){
mysqli_close($this->connection);
unset($this->connection);
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.