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
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
The following code successfully connects me to my mySQL database.
$conn = mysql_connect("localhost", "root", "") or die("Failed to Connect!");
$db = mysql_select_db("MyDB");
I have been experimenting on localhost using XAMPP and phpmyadmin, and everything works correctly. However, today I uploaded my website to my domain (I have bought a domain and web hosting through GoDaddy) and I keep getting "Failed to Connect!" whenever this code runs. The HTML/CSS work correctly, but I cannot connect to mySQL. How can I connect to my database on the server?
You'll need to change your connection information here:
mysql_connect("localhost", "root", "")
to include your GoDaddy database details instead. Contact GoDaddy for more information on what to use for your account.
You have not mentioned anything about MySQL database hosting.
If you are hosting the database also in godaddy, you should be able to get the connection string and host name from information mentioned here
First, You need to create database in your hosting server (phpmyadmin cpanel) and supply details in your database connect file.
**Looking at your database credentials I can say that you haven't created one yet since
the username is definitely not 'root' and password will be required.**
Sample db_connect file:
<?php
$hostname = 'e.g: internal-ra.db-server-123';
$username = 'e.g: serverone1234d4';
$password = 'e.g: your_own_password';
try {
$pdo = new PDO("mysql:host=$hostname;dbname=database_name_here", $username, $password);
/*** echo a message saying we have connected ***/
}
catch(PDOException $e){
echo 'Exception -> ';
var_dump($e->getMessage());
}
?>
And try to use so less as possible the key DIE()
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.