Usually, we get MySQL error query PHP mainly due to incorrect data in the PHP file or otherwise due to the permission or the ownership error.
Fixing the MySQL error query PHP
There might be many reasons to occur the error Message.
Incorrect details in the PHP file
By providing the wrong information may lead to the MySQL error. so you must provide the valid details in the PHP file.
One of our client reports has been displayed below.
// Check connection
echo "Failed to connect to MySQL: " . mysqli_connect_error();
// Perform a query, check for error
if (!mysqli_query($con,"INSERT INTO Persons (FirstName) VALUES ('Glenn')"))
echo("Error description: " . mysqli_error($con));
The error was caused due to the wrong username provided under my _user section. you can fix the error by updating the username in the file.
Error due to incorrect permission.
In order to load a file properly on the server, you must provide the correct permission to the files. If any incorrect permission is set for the file that leads to an error.
All the files were required to have 644 permission. Run the following command to change the permission.
chmod 644 filename
Set the correct permission of the folder in which these files reside.Run the following command to change the folder permission
chmod 755 folder_name
The ownership plays a prominent role, where its file can be either username or the root of the server. For all, you need to set the right one with the proper functionality of the file.
Run the commands to change the ownership of the file.
chown username:username filename
Run the command to set ownership of a file as root.
chown root:root filename
Mysql server down
Ensure the Mysql server is running well. If the server is down then the PHP file wouldn’t work.
Run the command and check the Mysql status.
systemctl status mysql
If suppose the MySQL server is down and gets an error you need to restart the MySQL server by running the following command.
systemctl restart mysql
In brief, the MySQL error query PHP is mainly caused to incorrect database details in the PHP or it might be due to incorrect permission or incorrect ownership in the files. you can take assistance from our technical team support to fix the error under the server management plan.