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'm using Jetbrains and Mysql to work on this practical project, but when I connect to the mysql
database it gives me the following error:
C:\wamp64\bin\php\php5.6.40\php.exe C:\wamp64\www\Social_Network\Includes\connection.php
Connection failed: SQLSTATE[HY000] [1049] Unknown database 'social_network'
Process finished with exit code 0
I made sure several times that the database name is the same name and there are
no spelling errors at all (I copy pasted it from the database)
Here's my code:
<?php
$servername = "localhost";
$username = "root";
$password = "";
try {
$conn = new PDO("mysql:host=$servername;dbname=social_network", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
} catch (PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
//?>
Welll, there's little that can be done about it. MySQL thinks that the database does not exist.
is the server the correct one?
is the case sensitivity set correctly? "Social_Network" and "social_network" might be considered different.
can you access the database with those parameters using a different tool (e.g. HeidiSQL, SQLYog, SQLterm, in a pinch even phpMyAdmin)?
Actually, JetBrains PHPStorm has a SQL terminal utility that can diagnose the connection. You may want to use it (once it knows what database you're connecting to, it will also warn you of several possible errors such as using the wrong table name or column name).
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
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.