-5

I have mysql database with some tables. In php script exist this code

$data = mysql_query("SELECT * FROM table1 WHERE Id='".$id."' LIMIT 1");

and it works fine.

Now i add 1 new table and mysql_query get:

Error #1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INSERT INTO...

The Php script work with this syntax

SELECT * FROM `table2` WHERE Id='".$id."' LIMIT 1

Without this symbol ' script return error message. How can it be, select query to table1 works fine and similar query don't work without ' symbol around table name?

Theodore R. Smith
  • 21,848
  • 12
  • 65
  • 91
Arman
  • 113
  • 1
  • 9

2 Answers2

0

I think that the table name of your code not table1 and table2. The reson for this error could be that your table name is a "reserved word"

Example:

It works

Select * from table1 

The following example will get error because using of reserved words

Select * from update

To correct this the word update must be written between apostrophe `

Basti
  • 261
  • 3
  • 7
0

There are a GREAT many problems with your question, and I am very afraid that people will close it because of your extremely poor coding quality rather than use it as a teaching lesson. Here we go!!

1. Stop using mysql_ functions!!

mysql_ functions have been deprecated for over ten years now. Using them is beyond negligent. They have been deprecated for at least three years and are no longer present in modern PHP.

Use PDO instead. http://www.phpro.org/tutorials/Introduction-to-PHP-PDO.html

2. Stop using SQL injections!! Start using prepared statements.

This is why your query is failing.

Implement the selected answer in How can I prevent SQL-injection in PHP?

3. Grow your PHP skills.

Your code is of such low quality, you absolutely must start growing today. You are only making the Web worse by continuing such practices, and undoubtedly, if you're doing stuff that has been advised, strongly, against for the better part of 12 years, you're doing other really bad things as well.

So please start learning again, today!

Community
  • 1
  • 1
Theodore R. Smith
  • 21,848
  • 12
  • 65
  • 91