MySQL - Couldn't Connect Unknown Database 'Databasename' Error

Results are different in PHP script and PHPMyAdmin

It's either a spelling error or PHP code and phpmyadmin are simply using different database credentials. It 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 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

Warning: mysqli_connect(): (HY000/1049): Unknown database in mac terminal only

Is your PHP environment the same as your browsers PHP? command line php.ini can differ from, for example, your XAMP or other installed webserver

I would recommend checking this first

for me this did the trick

edit your .bash_profile file like

export PATH=/Applications/MAMP/bin/php/php7.1.1/bin:$PATH

edit the path to your PHP bin from your XAMP

PDO_Construct: Unknown databasename error

1.; instead of , needed between dbname and host

2.Also database name is not correct (it need to be myDB) As you said that $conn = mysqli_connect("localhost","myusername","mypass","myDB") is working fine.

$pdo = new PDO("mysql:dbname=myDB;host=localhost","mysusername","mypass",array(PDO::ATTR_PERSISTENT => true));

error connecting to database with mysqldriver

You might want to specify the protocol (like 'tcp'), instead of localhost directly.

See those examples:

user:password@tcp(localhost:5555)/dbname

In your case:

username@tcp(localhost)/my_db

Note, if you use the default protocol (tcp) and host (localhost:3306), this could be rewritten as

user:password@/dbname

ERROR 1049 (42000): Unknown database

blog_development doesn't exist

You can see this in sql by the 0 rows affected message

create it in mysql with

mysql> create database blog_development

However as you are using rails you should get used to using

$ rake db:create

to do the same task. It will use your database.yml file settings, which should include something like:

development:
adapter: mysql2
database: blog_development
pool: 5

Also become familiar with:

$ rake db:migrate  # Run the database migration
$ rake db:seed # Run thew seeds file create statements
$ rake db:drop # Drop the database


Related Topics



Leave a reply



Submit