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.
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
PHP Domdocument - Get HTML Source of Body
How to Get/Set Session_Id() or Should It Be Generated Automatically
PHP Get Height and Width in PDF File Proprieties
Replace the Last Comma with an & Sign
Symfony & Guard: "The Security Token Was Removed Due to an Accountstatusexception"
Html5 Input Type File's Multiple Attribute Not Working in Ie
How to Avoid Using PHP Global Objects
Mod_Rewrite to Remove .PHP But Still Serve the .PHP File
PHP - Override Existing Function
Laravel Expected Response Code 250 But Got Code "530"
Wordpress Remove Robots Meta Tag Noindex
Php: What Is the Purpose of Session_Name
PHP JSON_Encode and JavaScript Functions
Implementing "Update If Exists" in Doctrine Orm
PHP MySQLi Commands Out of Sync; You Can't Run This Command Now