6

I installed WAMP server few hours ago into my Windows 10 64-bit computer. I used phymyadmin to create a database named 'testdb' and tried to connect to it with a php file. I am sure that I created the database, but it returns this error:

"Warning: mysqli_connect(): (HY000/1049): Unknown database 'testdb' in C:\wamp64\www\projects\index.php on line 7"

Here is the php file.

<?php
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_NAME', 'testdb');

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

My problem is kind of similar (but not exactly same) to the following question.:

Database is created but return error as unknown database

(However in that case, the database was not created by the original poster. In this case, I am pretty sure I have created the database.)

Additional information

  1. All wamp services are running.
  2. 'Root' user has privileges to access the database.(proof)
  3. MySQL console confirms the existence of the database. (proof)
  4. Wamp Server version 3.2.2.2
John Conde
  • 217,595
  • 99
  • 455
  • 496

1 Answers1

9

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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
John Conde
  • 217,595
  • 99
  • 455
  • 496
  • Thanks a lot! When I upload the files to hosting server, do i still need to specify the port? Or is this a wamp only thing? – Chathindu Nethmina May 26 '20 at 18:15
  • 1
    Although it is possible the port will be different on your production server, it is unusual for it to not be the standard `3306` and when it is not, it usually is made obvious by the web host. – John Conde May 26 '20 at 18:16
  • 1
    You can switch WAMPServer to use MySQL as the default, and then it will use the default port of 3306. THen you wont have to change anything. Although the other params will probably change so you should expect to have to maintain this code. **You will never be allowed to use `root` on a hosted server** or, hopefully a user account without a password – RiggsFolly May 26 '20 at 18:16
  • @RiggsFolly - Even better. And [here are the instructions for how to do it](https://dba.stackexchange.com/questions/261913/how-to-switch-from-mariadb-to-mysql-in-wamp-server). – John Conde May 26 '20 at 18:18
  • Or you can read the file called `mariadb_mysql.txt` in the WAMP folder – RiggsFolly May 26 '20 at 18:19
  • Anyone know WHY this has changed? What's the point of it in this case? – bestprogrammerintheworld May 26 '20 at 18:31