-4

i have tried every solutions answered here and on many other forums but all in vain I still got error when connecting to mysql data base using php

Could not connect: Access denied for user 'root'@'localhost' (using password: NO)

here is my php script

<?php 
$database="test"; //database name 
$Uname=$_POST['username'];//this values comes from html file after submitting 
$Pwd=$_POST['pwd'];    
$con = mysql_connect("127.0.0.1","root" ,"zain");//for wamp 3rd feild is balnk 
if (!$con)    
{     
die('Could not connect: ' . mysql_error());     
} 
mysql_select_db("$database", $con); 
$query = "INSERT INTO data (UserName,Password)VALUES ('$Uname','$Pwd')"; 
mysql_query($query);
echo "<script type='text/javascript'>\n";
echo "alert('you are Succesflly registered');
\n";
echo "</script>";
mysql_close();

and my php.config file is this

/* Server: localhost [1] */
$i++;
$cfg['Servers'][$i]['verbose'] = 'localhost';
$cfg['Servers'][$i]['host'] = 'localhost';
$cfg['Servers'][$i]['port'] = '';
$cfg['Servers'][$i]['socket'] = '';
$cfg['Servers'][$i]['connect_type'] = 'tcp';
$cfg['Servers'][$i]['extension'] = 'mysqli';
$cfg['Servers'][$i]['auth_type'] = 'config';
$cfg['Servers'][$i]['user'] = 'root';
$cfg['Servers'][$i]['password'] = 'zain';
$cfg['Servers'][$i]['AllowNoPassword'] = false;
John Conde
  • 217,595
  • 99
  • 455
  • 496
Zak
  • 101
  • 3
  • 1
    You might want to check http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php/12860046#12860046 and http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php/60496#60496 as you are using deprecated functions (that have been discouraged for like the past 5 years) and your code is open to SQL injection, meaning malicious users can simply alter or even delete your data. – Oldskool May 10 '14 at 13:33
  • Create a new user through phpMyAdmin and give it full privileges to the required database or for all databases. Then in config.inc.php write the username and password as you gave for new user. – Susheel Singh May 10 '14 at 13:38
  • Now change your password – Strawberry May 10 '14 at 14:23

2 Answers2

1

You need to do following steps:

  1. create new database 'test'. This you must already have
  2. Create new user root
  3. give to the new user 'All Privileges' access to perform actions on the database, like insert, delete, connect etc. I believe this is what you are missing.
  4. You can do this from phpmyadmin -> privileges -> edit privileges.

enter image description here

Aris
  • 4,643
  • 1
  • 41
  • 38
0

Firstly, you should be sanitizing the user input. Connecting to a database based on a user name that is not escaped can be dangerous for security.

Another point is that I don't think you quite understand how connecting to a database works.

mysql_ is in the deprecation process and is subject to be deleted in later versions of PHP. It's recommended that you use mysqli instead.

If you use the object oriented mysqli you would connect to a database like so:

$con = new mysqli($host, $user, $pass, $db);

In your case, you would use

$con = new mysqli("127.0.0.1", $_POST['username'], $_POST['pwd'], "test");

Do not expect this to work for you, your question in its current state is too localized to receive an answer you're after.

EDIT: I noticed your database is called test - why weren't you trying to connect to that? What is this zain all about?