Jump to content

Can't open connection to Mysql


tsz

Recommended Posts

Hello,

I recently installed Xampp and modified it's 80 and 443 default ports into other ports.

I have also set an mysql password via the Xampp localhost manager.

Now, I have rote an open_connection class in order to connect to the database but I get an credential error:

Warning: mysql_connect(): Access denied for user 'root'@'localhost' (using password: YES) in C:\xampp\htdocs\CMS\models\dbModel.php on line 19
failed to connect MYSQL Server. Please contact your website manager for futher assistance

Here is the class I have wrote:

 

 

class dbmodel
{
    private 
$connection;
    private 
$SelectDB;
    
    function 
__construct(){
        
$this->open_connection();
        
//$this->select_db();
    
}
    
    public function 
open_connection()
    {


        
defined('DB_HOST') ? null define('DB_HOST''localhost'); 
        
defined('DB_USER') ? null define('DB_USER''root');
        
defined('DB_PASSWORD') ? null define('DB_PASSWORD''1234');
        
defined('DB_NAME') ? null define('DB_NAME''cms');
        
$this->connection mysql_connect(DB_HOSTDB_USERDB_PASSWORD);
        
        if(!
$this->connection)
        {
            die(
'failed to connect MYSQL Server. Please contact your website manager for futher assistance');
        }
        else
        {
            
$SelectDB mysql_select_db(DB_NAME$this->connection);
            if(!
$SelectDB){
                die(
"Can't Select a database");
            }
        }
    }


    
}  

 

what should i do?  :-\  Is the problem in the class or in the configuration of XAMPP?

Help plz. ty :)

Link to comment
Share on other sites

As you said, it's a credential error. Which means root@localhost with password "1234" was not allowed to connect. Which probably means the password is wrong.

 

Don't use the root account to begin with. Create a new user with the permissions you need: connect as root successfully and run the query

GRANT SELECT, INSERT, UPDATE, DELETE ON cms.* TO usernamegoeshere@localhost IDENTIFIED BY "password goes here"
That will create a user with the common privileges; you won't be able to use it to do things like CREATE TABLEs or ALTER TABLEs, but those aren't the kinds of things you should be doing with this user anyways (temporarily use the root account for that).

Then, of course, change the credentials in your code to match.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.