2

I am trying to connect to my database through php. I exported my php database as an sql file as told. I tried to go to connect to it and show a message that i am connect. However I'm not.

Im running my php in netbeans and the file is located in wamp > www

it says ERROR: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES)

<?php
try {
$conn = new 
PDO('mysql:host=localhost;dbname=isad235', "root", 
"root");
echo "CONNECTED";
$sql = "SELECT * FROM members";
foreach($conn->query($sql) as $row)
{
  echo $row;
}
}catch(PDOException $e)
{
 echo 'ERROR: '.$e->getMessage();
}
?>

3 Answers 3

3

this is working for me. It it doesnt work you need to check your credentials.

class db {
    public static function dbFactory($host, $dbase, $user, $pass) {
        $pdo = new PDO("mysql:host=$host;dbname=$dbase", $user, $pass);
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);        
        return $pdo;
    }
}
$db = db::dbFactory('localhost','mydbname','myusername','mypassword');

by the way, you cannot connect to an sql file. You need to import it into your dbms. After that you should be able to connect.

Sign up to request clarification or add additional context in comments.

2 Comments

How do i put the sql file into my dbms? on wamp
a simple way is to use phpmyadmin. I dont know what you have installed on your machine to get a "wamp". But you should be able to open http://localhost/phpmyadmin in your browser. Login, create a new database, select the database, click on import, select your file and the database should be imported to your dbms.
0

If you want to import *.sql files via PHP, you should run following MySQL query in exec(); PHP function :

exec('mysql db_name < script.sql);

Comments

0

You need to create a user then. The error message is there to help you - work out what it it saying and do something about it. Sounds to me like you haven't got a clue about the database behind your code. You have to create a database, then create users to use the database. You look like you are using the default root user - are you sure that your version of the database actually has those settings? Some versions of MySQL have root and no password, others have root and password of root. Check out what yours is.

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.