Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating a MySQL column that contains dot (.) in its name

Tags:

php

mysql

I have columns that their names have the structure nameUser.Name, but I'm having trouble updating them. I've tried a few possibilities:

// Ideally, I'd like to do this (since the User.Name is 'dynamic', ie, it depends
// on who is logged in):
$userLogged = 'Some.User';
$columnName = 'name' . $userLogged;
mysql_query("UPDATE Industries SET '$columnName'='$name' WHERE id='$id'");
// Another try:
mysql_query("UPDATE Industries SET $columnName='$name' WHERE id='$id'");
// Alternatively, if the above cannot be achieved:
mysql_query("UPDATE Industries SET 'nameSome.User'='$name' WHERE id='$id'");
// Yet another try:
mysql_query("UPDATE Industries SET nameSome.User='$name' WHERE id='$id'");

Non of the above works, however. Why?

like image 664
mrinterested Avatar asked Apr 19 '12 13:04

mrinterested


2 Answers

Because that is also the syntax for database.table.column. Youll have to quote them like

`nameUser.name`

Though really, if you created/designed the db you should never use column names like that. Its just a horrible idea.

like image 197
prodigitalson Avatar answered Oct 03 '22 22:10

prodigitalson


Instead of using single-quotes for the column name, use backticks (on most keyboards, to the left of the 1 key).

Like this:

mysql_query("UPDATE Industries SET `nameSome.User`='$name' WHERE id='$id'");
like image 26
Travesty3 Avatar answered Oct 04 '22 00:10

Travesty3