1

I'm trying to setup a new local web server. Apache is already running and php is working. Mysql is installed. I can run mysql through the MySQL workbench. However, I am not able to connect to the MySQL from the php document.

First, the guides I looked at said to add the extension in php.ini. I went to php.ini-development and uncommented the line:

extension=mysqli

After restarting the server this didn't help. I also tried variations like extension=php_mysqli, and php_mysqli.dll.

I do in fact have the mysql dll on my computer. It is in:

C:\php\ext\php_mysqli.dll

My computer says the dll has not been accessed or modified since I downloaded it, so the Apache hasn't touched it at all.

Here is the code:

<?
$servername = "localhost";

$username = "root";

$password = "*************";

$conn = new mysqli($servername, $username, $password);

if ($conn->connect_error) {

  die("Connection failed: " . $conn->connect_error);

}

echo "Connected successfully";

?> 

I get this error:

Fatal error: Uncaught Error: Class 'mysqli' not found in C:\Websites\x.php:14 Stack trace: #0 {main} thrown in C:\Websites\x.php on line 14

The MySQL is set to port 3306, and mysqli.default_port is set to 3306 in httpd.conf.

This is Windows 10. The MySQL version is 8.0.2. How can I make PHP use the mysql library/talk to MySQL?

EDIT: Not the same as How to solve "Fatal error: Class 'MySQLi' not found"?. I have tried the solutions there, but none of the answers worked so far.

Vincent
  • 139
  • 1
  • 5
  • Does this help? https://stackoverflow.com/questions/666811/how-to-solve-fatal-error-class-mysqli-not-found (You need to set the extension directory in php.ini) – Neville Kuyt Aug 14 '20 at 04:29
  • @Vincent hello, vincent, could you format you question? – spike 王建 Aug 14 '20 at 04:31
  • Does this answer your question? [How to solve "Fatal error: Class 'MySQLi' not found"?](https://stackoverflow.com/questions/666811/how-to-solve-fatal-error-class-mysqli-not-found) – kmoser Aug 14 '20 at 06:36
  • So, I looked at the post. It has 2 main answers: have the extension uncommentd. (I've tried this.) Also, have Mysqli installed. Since I have the dll file, I'm assuming I have it installed? This: https://www.php.net/manual/en/mysqli.installation.php Says to install the windows binary, but I'm not sure if that's something different from php itself. So, the post didn't solve the problem. – Vincent Aug 14 '20 at 19:25

1 Answers1

2

First, be sure you're editing the "right" php.ini file. There can be several php.ini files on your system so execute this PHP code to see which php.ini is loaded in the context of your database connection script:

var_dump(php_ini_loaded_file());

Then, try to specify in this php.ini the absolute path to MySQLi DLL file like this:

extension=C:\php\ext\php_mysqli.dll

Note: Don't forget to restart Apache server after the modification of the php.ini.

  • Thanks so much, it just connected! Now I see there were two errors. First, I was in php.ini-development instead of php.ini. Second, it wanted the absolute path to the dll instead of just the dll name. – Vincent Aug 14 '20 at 22:14