Click here to Skip to main content
15,901,035 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '$query = "SELECT * FROM posts WHERE post_catecory_id = $post_category_id "' at line 1

i get this type of error, can anyone tell me how to solve this problem.

What I have tried:

i don't know how to solve this
Posted
Updated 14-Jun-20 21:16pm

The problem is that the query depend on value of variables, we have no idea of what is the real query:
PHP
$query = "SELECT * FROM posts WHERE post_catecory_id = $post_category_id "

Not necessary a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]
 
Share this answer
 
Comments
Maciej Los 15-Jun-20 3:20am    
5ed!
Patrice T 15-Jun-20 4:06am    
Thank you
Follow the steps from: MySQL Error 1064: You have an error in your SQL syntax | InMotion Hosting Support Center[^]

Quote:
Missing Data

Sometimes data can be missing from the database. This causes issues when the data is required for a query to complete. For example, if a database is built requiring an ID number for every student, it is reasonable to assume a query will be built to pull a student record by that ID number. Such a query would look like this:
SQL
SELECT * from students WHERE studentID = $id


If the $id is never properly filled in the code, the query would look like this to the server:
SQL
SELECT * from students WHERE studentID =


Since there is nothing there, the MySQL engine gets confused and complains via a 1064 error.

How to fix it:
Hopefully, your application will have some sort of interface that will allow you to bring up the particular record and add the missing data. This is tricky because if the missing data is the unique identifier, it will likely need that information to bring it up, thus resulting in the same error. You can also go into the database (typically within phpMyAdmin) where you can select the particular row from the appropriate table and manually add the data.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900