Log in

View Full Version : Resolved sql query number vs string



ggalan
12-21-2010, 04:36 PM
when i update a table like this, it worls


UPDATE tableName SET itemID=523 WHERE descript_1 LIKE 'nameOfItem';


but when i update it as a string, it doesnt work


UPDATE tableName SET differentItemID='someString' WHERE descript_1 LIKE 'nameOfItem';


what is going on?

fastsol1
12-21-2010, 04:55 PM
What is the string you are trying to update? You can always add

or die(mysql_error())
to the end of your query and that will give you an error if the query is failing due to a snytax error in the string or otherwise.

bluewalrus
12-21-2010, 05:33 PM
Is that column set to bit, int, float, or some other only numerical input?

fastols solution may give more results assuming you are using mysql, if not we will need to know what db you are using and what the column is set as.

ggalan
12-21-2010, 05:49 PM
it was set to "varchar(20)"

Schmoopy
12-21-2010, 07:30 PM
And did you get anything from mysql_error()? Assuming you're using MySQL...

If there's no error, what's it updating that field with?

ggalan
12-21-2010, 07:36 PM
#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 'differentItemID='someString' WHERE descript_1 LIKE 'nameOfItem'' at line 1

Schmoopy
12-21-2010, 08:06 PM
Try using backticks for the column name:



UPDATE tableName SET `differentItemID`= 'someString' WHERE descript_1 LIKE 'nameOfItem';


I can't imagine that this would cause the problem, are you sure the code above is exactly what's going into the query? (minus the backticks).

Try echoing the SQL out just before you execute it.

From what you've posted, you should be getting an error along the lines of:


"SQL Error (1146): Table 'databaseName.tableName' doesn't exist".

(I executed the above query through HeidiSQL)

ggalan
12-21-2010, 09:07 PM
the backticks solved it! weird
i always thought that ` was the same as '
how do i get those backticks from my keyboard?

Schmoopy
12-21-2010, 09:18 PM
Not sure what keyboard you have, but on mine (American English), the backtick is to the left of the "1" key. Should have like 3 symbols on it.

If you're not using this type of keyboard, then see if anyone else knows where it is.

ggalan
12-21-2010, 09:23 PM
got it `, lol
but back to the original question, why would i need those backticks for the sql query to work?

traq
12-21-2010, 09:27 PM
the backticks tell the SQL engine that the word contained within is a literal (e.g., the name of a field or table) and not a keyword or command. it usually makes no difference whatsoever if you use them or not, but it does prevent conflicts -and makes your sql more forward-compatible.

in this particular case, I'm not sure why it made the difference. it really shouldn't have. but hey, you don't argue with success...! :D