How to Solve This "Warning: MySQLi_Connect(): (Hy000/1049): Unknown Database" Problem

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

How can i solve this Warning: mysqli_connect(): (HY000/1049): Unknown database problem?

New versions of WAMP seems to install both MySQL and MariaDB. And when I logged in through the phpMyAdmin, I saw that the MySQL is using the port 3308.

Sample Image


So in my PHP code, instead of using just localhost for my database hostname, I added the port to it like this: localhost:3308 and it worked.

$db = new mysqli('localhost:3308', 'root', 'password', 'db_name');

Database Selection Failed Unknown database 'login'

You dont have a database called shoolbrk, your database appears to be called login.

Also you you should use mysqli_connect_error() to see connection errors and not mysqli_error($connection). Thats for all other errors other than connection errors.

You can also simplify the connection script as follows

<?php
$connection = mysqli_connect('localhost', 'root', '', 'login');
// database name ^^^^^
if (!$connection){
echo $connection ->connect_error;
}

A better solution would also be to not die() or show errors to the world, instead add mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); to the connection script.

Port number issue

See this answer https://stackoverflow.com/a/62831626/2310830 which describes how WAMPServer manages MySQL and mariaDB port number usage. And how it will allow you to switch the default to MySQL so it uses port 3306 which will allow you to write your code in a way that will run anywhere without having to change the port number. As most hosting companies will have either MySQL or mariaDB listening on 3306

Database exists but returns an error saying Unknown Database

In the newer versions of Wampserver, the port for MySQL has changed from 3306 to 3308 (you can see it in your first screenshot). You will need to update your connection to specify that port. Otherwise you will be hitting the MariaDB installed with WAMP which does not have that database within it.

define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_NAME', 'testdb');
define('DB_PORT', 3308);

$link = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME, DB_PORT);

As pointed out in the comments, it is also possible to make MySQL your default database which would also solve your problem. You can get the instructions from the DBA Stack Exchange site.

Failing to connect to database (HY000/1045), trying to connect to a database using php mysqli_connection

This error appears because you have passed invalid credentials (user name and/or password or maybe the hostname) when trying to connect to the database.
You have to check the valid credentials by yourself. If you use phpmyadmin it should be in tab "User Accounts".

see this reference may help you https://dev.mysql.com/doc/refman/5.7/en/creating-accounts.html



Related Topics



Leave a reply



Submit