简体   繁体   中英

Unable to Connect to MySQL on Apache Server

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM