how to fix this??

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
yhi
Forum Newbie
Posts: 2
Joined: Tue Apr 14, 2015 1:19 pm

how to fix this??

Post by yhi »

Error:



Connection failed: Access denied for user 'uname'@'localhost' to database 'test_db'

m using code from w3 school

Code: Select all

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// sql to create table
$sql = "CREATE TABLE MyGuests (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP
)";

if ($conn->query($sql) === TRUE) {
    echo "Table MyGuests created successfully";
} else {
    echo "Error creating table: " . $conn->error;
}

$conn->close();
?>




i think i have to give privilege to user

how to do it ??
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: how to fix this??

Post by requinix »

Either your username is wrong, the password is wrong, or the user doesn't have access.

Run this query in some database thing, like phpMyAdmin, and post the result.

Code: Select all

SHOW GRANTS FOR uname@localhost
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: how to fix this??

Post by Celauran »

yhi wrote:m using code from w3 school
PHP: The Right Way
yhi
Forum Newbie
Posts: 2
Joined: Tue Apr 14, 2015 1:19 pm

Re: how to fix this??

Post by yhi »

requinix wrote:

Code: Select all

SHOW GRANTS FOR uname@localhost

query not workng :/
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: how to fix this??

Post by requinix »

Not working how?

Did you put the right username in there? Your error message said "uname" but your code, no doubt edited before you posted it, says "username".
Post Reply