JONATHAN MARTINI
JONATHAN MARTINI

Reputation: 13

PHP script cannot access database in sql

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>";
?>

Upvotes: 0

Views: 1543

Answers (2)

Nocturnal Penguin
Nocturnal Penguin

Reputation: 153

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)

Upvotes: 2

Ben P. Dorsi-Todaro
Ben P. Dorsi-Todaro

Reputation: 321

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

Upvotes: 0

Related Questions