When using PHP for MySQL database operations, we often encounter the error "PHP Fatal error: Call to undefined method mysqli::stmt_init()". This error is usually due to PHP not loading the MySQL extension library or MySQL correctly. Caused by incompatible extension library versions. In this article we will explain how to solve this problem.
Step one: Check whether PHP correctly loads the MySQL extension library
In PHP, we can view the current PHP configuration information through the phpinfo() function. Open the terminal and enter the following command to view the current PHP configuration information:
php -r "phpinfo();"
After executing this command, the PHP configuration information will be output. We need to confirm whether the MySQL extension library has been loaded.
You can search for mysqli through Ctrl F. If it is not found, it means that the MySQL extension library is not loaded. You need to install the MySQL extension library first.
Step 2: Install the MySQL extension library
According to your actual situation, you can install the MySQL extension library in the following two ways:
sudo apt-get update sudo apt-get install php-mysql
sudo yum update sudo yum install php-mysql
If the above command fails When executing, you can choose to manually compile and install the extension. Take the Ubuntu system as an example:
sudo apt-get update sudo apt-get install php7.2-dev libmysqlclient-dev sudo pecl channel-update pecl.php.net sudo pecl install mysqlnd_ms
Note that php7.2-dev in the above command needs to be modified to the currently used PHP version. libmysqlclient-dev must be installed. If it is not installed, it will not be compiled successfully.
After the installation is completed, you need to add the following configuration information to php.ini:
extension=mysqlnd_ms.so
Step 3: Restart the Apache service
After installing the MySQL extension library, you need to Restart the Apache service to take effect:
sudo /etc/init.d/apache2 restart
sudo systemctl restart httpd
After completing the above three steps, run the program again and you will be able to connect normally The MySQL database is operated.
Summary:
When using PHP for MySQL database operations, it is easy to encounter the error "PHP Fatal error: Call to undefined method mysqli::stmt_init()". This error is usually This is caused by PHP not loading the MySQL extension library correctly or the MySQL extension library version being incompatible. To address this problem, we can confirm whether the MySQL extension library has been loaded correctly by checking the information returned by the phpinfo() function. If it is not loaded correctly, you need to install the MySQL extension library through the command line or manually compile and install it; after the installation is completed, you need to Restart the Apache service to take effect.
The above is the detailed content of Solution to PHP Fatal error: Call to undefined method mysqli::stmt_init(). For more information, please follow other related articles on the PHP Chinese website!