php - Unable to Connect to MySQL on Apache Server
Get the solution ↓↓↓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.
Answer
Solution:
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.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: call to a member function getclientoriginalname() on null
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.