0

I have a problem in my DB there is a fieldname 'show' in I have a php script that updates table or insert and everything is working to the field name 'show' even if I change it to 'showz' it works! I think it is a glitch here is a part of the php code!

mysql_query("INSERT INTO db.table (mid, name, show) VALUES ('".$row['mid']."',     '".$row['name']."', '".$row['show']."') ON DUPLICATE KEY UPDATE name = '".$row['name']."',  show = '".$row['show']."'");

I tried to put the show thing in 'show' , show , "show" nothing helps!

  • `show` keyword is reserved by *MySQL*, use `\`show\`` – mychalvlcek Dec 20 '12 at 14:36
  • 1
    You said you tried to put `show` into backticks \`show\`, which would be correct, as you can see in all three answers. Are you sure, you put **both** occurences of `show` into backticks? – Michel Feldheim Dec 20 '12 at 14:39

3 Answers3

4

that's because show is a reserved word (like insert, update, order etc). You can read about reserved words here: MySQL Reserved Words

In order to use a reserved word as a field name you need to escape it with backticks (`) like so:

mysql_query("INSERT INTO db.table (mid, name, `show`) VALUES ('".$row['mid']."',     '".$row['name']."', '".$row['show']."') ON DUPLICATE KEY UPDATE name = '".$row['name']."',  `show` = '".$row['show']."'");

This tells mysql to treat it as a field rather than use it in its reserved word meaning.

I find it's good practice to always escape my table and field names with backticks like so:

mysql_query("INSERT INTO `db`.`table` (`mid`, `name`, `show`) VALUES ('".$row['mid']."',     '".$row['name']."', '".$row['show']."') ON DUPLICATE KEY UPDATE `name` = '".$row['name']."',  `show` = '".$row['show']."'");
Stu
  • 4,160
  • 24
  • 43
4

AS show is a MySQL reserved word, you need to enclose it in backticks (`) if you use it as a table or column name

`show`
Mark Baker
  • 209,507
  • 32
  • 346
  • 385
3

you are using reserved keyword use backticks

Table 9.2. Reserved Words in MySQL 5.0.96

enter image description here

so use like

`show`

rather warping all colon name in backticks is good approach

mysql_query("INSERT INTO `db`.`table` (`mid`, `name`, `show`) VALUES ('".$row['mid']."',     '".$row['name']."', '".$row['show']."') ON DUPLICATE KEY UPDATE `name` = '".$row['name']."',  `show` = '".$row['show']."'");

Note

  1. The entire ext/mysql PHP extension, which provides all functions named with the prefix mysql_, is officially deprecated as of PHP v5.5.0 and will be removed in the future. So use either PDO or MySQLi

Good read

  1. The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead
Community
  • 1
  • 1
NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143