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();
}
?>
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.
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);
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.