try to set host in dsn of pdo like this:
<?php
/* Connect to an ODBC database using driver invocation */
$dsn = 'mysql:dbname=xxx_online;host=192.168.1.105;';
$user = 'username';
$password = 'password';
try {
$dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
?>
it works now. I misunderstood it.
The error means your username and/or password isn't correct, (or isn't actually set up on your server). The reason it works on your other server is because that user exists there.
You either need to check you have the right username and password set in your PHP file, or you need to create a new MySQL user, and connect with the new username and password for that server.
http://dev.mysql.com/doc/refman/5.1/en/adding-users.html
If you're uncomfortable doing this from the command line, you can install phpMyAdmin via apt-get and do it through a web interface.
Related
I am using PDO to connect to mySql database. I am not able to connect to any database that I create although I can connect to already created databases( already created by default). I am using wamp server.
<?php
try{
$dbh=new PDO("mysql:host=localhost;dbname=mydata","root","");
}catch(Exception $e){
die("ERROR: Couldn't connect. {$e->getMessage()}");
}
?>
If i substitute mydata with mysql which is previously created database in wamp server, then the code works perfectly. The only problem is with the databases that I create. I have tried giving mydata the same privileges as mysql database but it doesn't work.
It's either
a spelling error. Simply check the names again.
or the PHP code and PHPMyAdmin are connecting to different databases
The latter could happen, for example, if you have multiple database servers on your PC installed.
To get a proof, run the following query in phpmyadmin:
show databases;
And then run the same query in PHP, using either PDO:
$host = 'your db host';
$user = 'your db username';
$pass = 'your db password';
$pdo = new PDO("mysql:host=$host", $user, $pass, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
$databases = $pdo->query('show databases')->fetchAll(PDO::FETCH_COLUMN);
var_dump($databases);
or mysqli
$host = 'your db host';
$user = 'your db username';
$pass = 'your db password';
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = mysqli_connect($host, $user, $pass);
$databases = $mysqli->query('show databases')->fetch_all();
var_dump($databases);
and compare the output. It will show you that either there is a spelling error or indeed PHPMyAdmin and PHP are connected to different database servers.
Then you can check the configuration file in PHPmyAdmin to make sure it connects to the proper server
I am using PDO to connect to mySql database. I am not able to connect to any database that I create although I can connect to already created databases( already created by default). I am using wamp server.
<?php
try{
$dbh=new PDO("mysql:host=localhost;dbname=mydata","root","");
}catch(Exception $e){
die("ERROR: Couldn't connect. {$e->getMessage()}");
}
?>
If i substitute mydata with mysql which is previously created database in wamp server, then the code works perfectly. The only problem is with the databases that I create. I have tried giving mydata the same privileges as mysql database but it doesn't work.
It's either
a spelling error. Simply check the names again.
or the PHP code and PHPMyAdmin are connecting to different databases
The latter could happen, for example, if you have multiple database servers on your PC installed.
To get a proof, run the following query in phpmyadmin:
show databases;
And then run the same query in PHP, using either PDO:
$host = 'your db host';
$user = 'your db username';
$pass = 'your db password';
$pdo = new PDO("mysql:host=$host", $user, $pass, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
$databases = $pdo->query('show databases')->fetchAll(PDO::FETCH_COLUMN);
var_dump($databases);
or mysqli
$host = 'your db host';
$user = 'your db username';
$pass = 'your db password';
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = mysqli_connect($host, $user, $pass);
$databases = $mysqli->query('show databases')->fetch_all();
var_dump($databases);
and compare the output. It will show you that either there is a spelling error or indeed PHPMyAdmin and PHP are connected to different database servers.
Then you can check the configuration file in PHPmyAdmin to make sure it connects to the proper server
I am using PDO to connect to mySql database. I am not able to connect to any database that I create although I can connect to already created databases( already created by default). I am using wamp server.
<?php
try{
$dbh=new PDO("mysql:host=localhost;dbname=mydata","root","");
}catch(Exception $e){
die("ERROR: Couldn't connect. {$e->getMessage()}");
}
?>
If i substitute mydata with mysql which is previously created database in wamp server, then the code works perfectly. The only problem is with the databases that I create. I have tried giving mydata the same privileges as mysql database but it doesn't work.
It's either
a spelling error. Simply check the names again.
or the PHP code and PHPMyAdmin are connecting to different databases
The latter could happen, for example, if you have multiple database servers on your PC installed.
To get a proof, run the following query in phpmyadmin:
show databases;
And then run the same query in PHP, using either PDO:
$host = 'your db host';
$user = 'your db username';
$pass = 'your db password';
$pdo = new PDO("mysql:host=$host", $user, $pass, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
$databases = $pdo->query('show databases')->fetchAll(PDO::FETCH_COLUMN);
var_dump($databases);
or mysqli
$host = 'your db host';
$user = 'your db username';
$pass = 'your db password';
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = mysqli_connect($host, $user, $pass);
$databases = $mysqli->query('show databases')->fetch_all();
var_dump($databases);
and compare the output. It will show you that either there is a spelling error or indeed PHPMyAdmin and PHP are connected to different database servers.
Then you can check the configuration file in PHPmyAdmin to make sure it connects to the proper server
I am using PDO to connect to mySql database. I am not able to connect to any database that I create although I can connect to already created databases( already created by default). I am using wamp server.
<?php
try{
$dbh=new PDO("mysql:host=localhost;dbname=mydata","root","");
}catch(Exception $e){
die("ERROR: Couldn't connect. {$e->getMessage()}");
}
?>
If i substitute mydata with mysql which is previously created database in wamp server, then the code works perfectly. The only problem is with the databases that I create. I have tried giving mydata the same privileges as mysql database but it doesn't work.
It's either
a spelling error. Simply check the names again.
or the PHP code and PHPMyAdmin are connecting to different databases
The latter could happen, for example, if you have multiple database servers on your PC installed.
To get a proof, run the following query in phpmyadmin:
show databases;
And then run the same query in PHP, using either PDO:
$host = 'your db host';
$user = 'your db username';
$pass = 'your db password';
$pdo = new PDO("mysql:host=$host", $user, $pass, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
$databases = $pdo->query('show databases')->fetchAll(PDO::FETCH_COLUMN);
var_dump($databases);
or mysqli
$host = 'your db host';
$user = 'your db username';
$pass = 'your db password';
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = mysqli_connect($host, $user, $pass);
$databases = $mysqli->query('show databases')->fetch_all();
var_dump($databases);
and compare the output. It will show you that either there is a spelling error or indeed PHPMyAdmin and PHP are connected to different database servers.
Then you can check the configuration file in PHPmyAdmin to make sure it connects to the proper server
I recently installed the latest version of XAMPP and transferred my database over to it. I created my user accounts on phpmyadmin, however when i try to access the database with any user other than root through PHP I get:
Connection failed: SQLSTATE[HY000] [1045] Access denied for user
It does not work for both users with and without a password. I have tried flushing the privileges deleting and creating the users again but nothing seems to work.
Example connection code:
<?php
$dsn = 'mysql:dbname=test_db;host=127.0.0.1';
$user = 'test_user';
$password = 'test';
try {
$dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
?>
If you are SURE of your valid credential, it's maybe a charset problem ( due to your database transfert)
Try to go in phpmyadmin, and change the collation on operations tab to "t8_general_ci".
But, in most of time, your error is due to bad credential or bad privileges.
Check it too... But first :).