-1

Trying to connect to MySQL using this code

<?php
$username = "root";
$password = "";
$hostname = "localhost"; 

//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password) 
  or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
?>

but for password I am actually entering the password.

I know this is the right info because I connect to MySQL in Java with the info I am entering.

I even went in MySQL work bench and made a new user from scratch and it wouldn't work with the new user.

I am getting this error message

Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'root'@'localhost' (using password: YES) in my/path/phptest.php on line 7
Unable to connect to MySQL
Craig
  • 364
  • 1
  • 3
  • 25
  • is the host actually "localhost". That's the only thing I can see. As a side note, you should be using something other than mysql_ functions. There are way insecure. – xJoshWalker Jan 13 '15 at 02:11
  • 2
    ^^ They're not insecure if used correctly. But they are _deprecated_. New code should be written using MySQLi or PDO. – Michael Berkowski Jan 13 '15 at 02:12
  • A quick search for that error message on this site revealed 682 similar questions. Are you suggesting that __none__ of them have anything helpful? –  Jan 13 '15 at 02:12
  • possible duplicate of [MySQL - ERROR 1045 - Access denied](http://stackoverflow.com/questions/489119/mysql-error-1045-access-denied) – Kevin Brown-Silva Jan 13 '15 at 02:13
  • I browsed and did not see an answer that worked for me. All ones I viewed, the user did not know his information. I know the information I am using should be correct, as stated I can log in with Java using the information I am using.. – Craig Jan 13 '15 at 02:14
  • While your code here doesn't show it...The `mysql_` family of drivers for PHP bread a decades worth of programmers who wrote poorly secured code resulting in huge infections and massive botnets all over (that and pirated copies of windows with Rootkits). Please migrate to `mysqli_` and use `prepared statements()` for your safety and ours. – Ohgodwhy Jan 13 '15 at 02:30
  • try to create another user and grant permission to your db just to test if it works. – Maytham Fahmi Jan 13 '15 at 02:37

3 Answers3

0

Hello try with this brother....actually mysql is deprecated now we have to use mysqli

 <?php
 $con=mysqli_connect("localhost","root","your_password");

 if($con)
 {
 mysqli_select_db("your_db",$con);
 }
 ?>
Vamshi .goli
  • 522
  • 4
  • 13
-1

For one, you may not be entering the correct MySQL info.

But maybe not. If you are 100% sure the details are correct, try using this MySQLi script.

<?php
$con =     mysqli_connect("localhost","my_user","my_password","my_db");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
?>
prestotron
  • 35
  • 1
  • 7
  • I don't know, it's still giving me an error. If I am using MySQL workbench to manage my DB stuff, localhost is the right server info right? – Craig Jan 13 '15 at 03:13
-1
var $conLinks;
var $host = "localhost";
var $user = "root";
var $pass = "";
var $database = "test";
mysql_connect($host,$user,$pass) or die("error".mysql_error());
mysql_select_db($database);

OR

you can refer to http://www.w3schools.com/php/php_mysql_intro.asp

shankar.parshimoni
  • 1,289
  • 5
  • 22
  • 42