mysql_connect error

rajat44

Banned
Messages
24
Reaction score
0
Points
0
i'm creating a register script for my site and till yesterday it was working fine and i could take registrations.. now all it shows is:

Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'urworld'@'10.33.248.75' (using password: YES) in /home/urworld/public_html/config.php on line 10
cannot connect to server

The config.php file where i have the connection to the mysql serves goes like this:

<?

$host="localhost"; // Host name
$username="myusername"; // Mysql username
$password="mypassword"; // Mysql password
$db_name="database name"; // Database name
$tbl_name="table name"; // Table name

//Connect to server and select database.
mysql_connect("$host", "username", "password")or die("cannot connect to server");

mysql_select_db("database name")or die("connot find db");

?>



Please can someone please please help?

the url of the page which shows error on clicking submit is:

UrWorld/Register

Thank You..
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
MySQL usernames should be your cpanel username, followed by an underscore, followed by the name you created in cPanel. The above username ("urworld") thus looks incorrect.

The "Access denied for user" error has been covered in many threads. Search the forums to look for solutions to your problem. You just might learn a few things you weren't expecting.

The old mysql driver is outdated and has been supplanted twice over. You should update your code to use the new PDO driver. If you need a tutorial, try "Writing MySQL Scripts with PHP and PDO".

Please use
PHP:
, [html] or [code] tags (as appropriate) to delineate and format code.
 

bdistler

Well-Known Member
Prime Account
Messages
3,534
Reaction score
196
Points
63
Enter your user stuff then try this PHP code on your server...

PHP:
<?php
// THIS IS A TEST PHP SCRIPT TO TEST CONNECTING TO YOUR MySQL DATABASE
// DO NOT INCORPORATE INTO YOUR SITE UNTIL YOU HANDLE ERRORS AND 'PASSWORD' TYPE DATA in THE APPROPRIATE WAY
//
// was tested on servers skyy, and bour at x10hosting
//
// Make sure any PHP files have permissions set to 0644 (NOT 0666 or 0777) and subdirectories set to 0755
//
// There is a possibility your .htaccess file can cause problems
// some .htaccess directives that work on other sites do not work on x10Hosting 'free' sites
// cPanel's "IP Deny Manager" on x10Hosting 'free' sites makes the wrong code
//
// Some say x10hosting server boru is haunted.
//

$serverName = "localhost"; // MySQL server name
$user_name = "myCpanelName_databaseUsername"; // MySQL user name _ do not forget the underscore
$password = "myDatabasePassword"; // MySQL password
$db_name = "myCpanelName_databaseName"; // MySQL Database name - do not forget the underscore
$tableName = "tableName"; // MySQL Table name

// try get "MySQL link identifier"
$dbConn = mysql_connect($serverName, $user_name, $password) or die("Cannot connect to server<br />\n MySQL error ==>" . mysql_errno() . "<== : ==>" . mysql_error() . "<== <br />\n");
print "Connected to MySQL server successfully<br />\n";

// try connect to database
mysql_select_db($db_name, $dbConn) or die("Cannot connect to database<br />\n MySQL error ==>" . mysql_errno($dbConn) . "<== : ==>" . mysql_error($dbConn) . "<== <br />\n");
print "Connected to database successfully<br />\n";

// try to get info from table
$query = 'SELECT * FROM ' . $tableName . ' LIMIT 0, 1';
$result = mysql_query($query, $dbConn) or die("Cannot select anything from table<br />\n MySQL error ==>" . mysql_errno($dbConn) . "<== : ==>" . mysql_error($dbConn) . "<== <br />\n");

print "I connected to your server, connected to your database, and received info from your table<br />\n";
?>
 
Last edited:

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
rajat44, some warnings, should you be tempted to incorporate bdistler's script into your site (rather than using it as a one-off to test connecting to MySQL): you shouldn't use die when outputting HTML. You should also handle errors at the appropriate point, which isn't often when the error is detected. Better to hand off the error (exceptions were made for just this purpose).

Only admins should get to see the output of mysql_error, as it discloses too much information.

In short, the techniques in bdistler's script are all right for debugging (which is what the script is for) if you don't have any other way, but shouldn't be used in production code, not even as temporary measures.
 
Top