0

I am trying to connect to a MySQL database through a php script. It gives me this error from mysql_error(): Access denied for user '@localhost' to database 'userinfo'

userinfo is the database.

my script is this

<?php
$servername = "localhost";
$username = "root";
$password = "'mm'";
$database = "userinfo";
$conn = mysqli_connect($servername, $username, $password);

if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

echo "Connected successfully<br>";

mysql_select_db($database) or die(mysql_error());

echo "connected successfully to db:" . $database . "<br>";
?>
8
  • Is your password really 'mm' or mm? Commented Jun 19, 2015 at 2:20
  • i accidentally set it to 'mm'. the reason I set it to mm is bc this is my first time using it and didn't feel like making a long password. Commented Jun 19, 2015 at 2:24
  • You may have to add user privileges if you want external access to the DB: dev.mysql.com/doc/refman/5.1/en/adding-users.html Commented Jun 19, 2015 at 2:26
  • Are you using a hosting provider or did you setup mysql server and PHP on your own system. And if so are you using software like WAMP? I ask this because you can connect to DB's differently on hosting providers For example on hostgaator you would connect like username_dbasenamme and with godaddy it is something like db.server.com Commented Jun 19, 2015 at 2:27
  • I set it up o my own system using xampp Commented Jun 19, 2015 at 2:29

2 Answers 2

2

you are connecting using

mysqli_

function and selecting data with

mysql_

avoid using both at the same time. since they're incompatible. use the mysqli_ alternative instead

mysqli_select_db($conn, $database);

and

mysqli_error($conn)
Sign up to request clarification or add additional context in comments.

Comments

0

Please keep in mind this isn't the safest way. But since you have said your learning this it is a start.

<?php
$servername = "localhost";
$username = "root";
$password = "mm";

// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
?>

http://www.w3schools.com/php/php_mysql_connect.asp

To select data from the database

http://www.w3schools.com/php/php_mysql_select.asp

It appeared you where combining the old mysql in php with the new mysqli

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.