problem connecting to database :: Warning: mysql_connect() [

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
darren713
Forum Newbie
Posts: 5
Joined: Tue May 27, 2008 1:38 am

problem connecting to database :: Warning: mysql_connect() [

Post by darren713 »

Hello all
I have a problem connecting my msql db to the server. the db i use and have created works completely fine in my localhost however when i upload the files with the include to connection.php the realtime live sie comes up with

Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'root'@'localhost' (using password: YES) in /includes/connection.php on line 4
Database connection failed: Access denied for user 'root'@'localhost' (using password: YES)

Here is what i have tried so far.
gone into mysql console and ran this line

Code: Select all

SET PASSWORD FOR
'root'@'localhost' = OLD_PASSWORD('new_pass');
Also i tried the UPDATE and FLUSH PRIVALIGES cmd's to no joy

my constants.php file is correct to the mysql cmd like so

Code: Select all

<?php
 
 
define("DB_SERVER","localhost");
define("DB_USER", "root");
define("DB_PASS", "pxxxxxxx");
define("DB_NAME", "my-db");
 
?>
my connection.php looks like this

Code: Select all

<?php
require("constants.php");
//1. create a connection
    $connection = mysql_connect(DB_SERVER,DB_USER,DB_PASS); 
    if (!$connection) {
        die("Database connection failed: " . mysql_error());
    }
 
    // 2. Select a database to use 
    $db_select = mysql_select_db(DB_NAME,$connection);
    if (!$db_select) {
        die("Database selection failed: " . mysql_error());
    }
?>
I have spoke to my server administrator and they say its completely fine using a mysql db from another server as such so how or where am i going wrong because i am banging my head against the wall.

p.s the testing server url prefix is set to my local localhost
Last edited by Benjamin on Sun Jun 07, 2009 11:52 pm, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
akuji36
Forum Contributor
Posts: 190
Joined: Tue Oct 14, 2008 9:53 am
Location: Hartford, Connecticut

Re: problem connecting to database :: Warning: mysql_connect() [

Post by akuji36 »

Its not a good idea to have server name set to root.

Best change it to something else:

these files should help.

for your connection:

Code: Select all

require("constants.php");
 
// 1. Create a database connection
$connection = mysql_connect(DB_SERVER,DB_USER,DB_PASS);
if (!$connection) {
    die("Database connection failed: " . mysql_error());
}
 
// 2. Select a database to use 
$db_select = mysql_select_db(DB_NAME,$connection);
if (!$db_select) {
    die("Database selection failed: " . mysql_error());
}
 
constants

Code: Select all

require("constants.php");
 
// 1. Create a database connection
$connection = mysql_connect(DB_SERVER,DB_USER,DB_PASS);
if (!$connection) {
    die("Database connection failed: " . mysql_error());
}
 
// 2. Select a database to use 
$db_select = mysql_select_db(DB_NAME,$connection);
if (!$db_select) {
    die("Database selection failed: " . mysql_error());
}
 
Last edited by Benjamin on Sun Jun 07, 2009 11:52 pm, edited 1 time in total.
Reason: Changed code type from text to php.
Post Reply