When using the mysqli extension to connect and operate the MySQL database, sometimes you will encounter the error PHP Fatal error: Call to undefined method mysqli::prepare(). This error is usually caused by the following reasons:
For these reasons, the solutions will be introduced below.
First, we need to check whether PHP has the mysqli extension enabled. You can obtain PHP information about the mysqli extension through the phpinfo() function. The method is as follows:
1) Create a php file, named phpinfo.php, and copy the following code into it:
<?php phpinfo(); ?>
2) Upload this php file to the root directory of the website, and Enter http://yourdomain.com/phpinfo.php in the browser to access the file;
3) In the PHP information page, check whether the "mysqli" extension has been loaded. If not, you need to enable the mysqli extension in the php.ini file.
If you have confirmed that the mysqli extension is supported, but the PHP Fatal error: Call to undefined method mysqli::prepare() error still occurs , then you need to check whether the configuration of the mysqli extension is correct.
First, check whether the mysqli extension has been loaded correctly. You can confirm by looking for the following code in the PHP configuration file php.ini:
extension=mysqli.so
If the above code is not found, you need to load the mysqli extension in php.ini path to set.
Next, we need to check whether the parameters used when connecting to the database are correct. Normally, a mysqli connection requires 4 parameters: host name, user name, password, and database name. Make sure these 4 parameters are configured correctly. For example:
$host = 'localhost'; $username = 'user'; $password = 'pwd'; $database_name = 'testdb'; $conn = new mysqli($host, $username, $password, $database_name);
Finally, if the mysqli extension is not configured correctly, you can try manually configuring the mysqli version in code. For example:
$conn = new mysqli(); $conn -> init(); $conn -> options(MYSQLI_OPT_CONNECT_TIMEOUT, 5); $conn -> real_connect($host, $username, $password, $database_name);
When using the mysqli extension, if there is a syntax error in the PHP code, it will also cause PHP Fatal error: Call to undefined method Mysqli::prepare() error. Therefore, we need to check our code for syntax errors.
You can use a code editor or IDE tool to check the syntax, especially to check whether there are missing semicolons, mismatched braces, etc.
If the above measures still cannot solve the problem, you need to check whether the MySQL server is running and has been configured correctly.
First, check whether the MySQL server has been started. You can check on the terminal using the following command:
sudo service mysql status
If you have used MariaDB, you can try the following command:
sudo service mariadb status
If the MySQL or MariaDB service is running, you need to check whether the MySQL database has been Configure correctly. You can use the MySQL client to log in to the MySQL database and check whether the required database and tables exist.
mysql -u username -p
Enter your password, and then view the current database:
SHOW DATABASES;
If your database is not in the list, you need to create the database manually.
If you have configured the MySQL server and database correctly, then the problem is most likely in the PHP part. You can check whether the PHP code calls the mysqli class and its methods correctly.
Summary
When using the mysqli extension, if the error PHP Fatal error: Call to undefined method mysqli::prepare() occurs, you need to first check whether PHP supports the mysqli extension and whether the mysqli extension It is loaded and configured correctly, there are no syntax errors in the PHP code, and the MySQL database is configured correctly. Only when the above problems are eliminated can you continue to troubleshoot other problems.
The above is the detailed content of Solution to PHP Fatal error: Call to undefined method mysqli::prepare(). For more information, please follow other related articles on the PHP Chinese website!