mysql syntax question

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
ssand
Forum Commoner
Posts: 72
Joined: Sat Jun 22, 2002 9:25 pm
Location: Iowa

mysql syntax question

Post by ssand »

I have the following mysql statement:

Code: Select all

$sql = "UPDATE inv_type SET inv_type_name='$type_name', inv_type_function='$type_function'
						WHERE inv_type_name=$original_name";
Which appeared to be working correctly, as it wouldn't create an error. But when veiwing the database it didn't actually change anything. Eventhough I was displaying all values directly before the UPDATE... so the variables were correct.

But when I change the WHERE to read:

Code: Select all

$sql = "UPDATE inv_type SET inv_type_name='$type_name', inv_type_function='$type_function'
						WHERE inv_type_name='$original_name' ";
With single quotes around the $original_name it works correctly.

I assume that I should have the single quotes around all variables?

Thanks

EDIT: The reason I ask is because I copied the first $sql statement from a different page changed some values and the original statement didn't use single quotes around the last variable. So, I'm wondering why one works without the quotes and one doesn't.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

that would depend on what is sent via $original_name.. if it's not a number you will have a syntax error.
ssand
Forum Commoner
Posts: 72
Joined: Sat Jun 22, 2002 9:25 pm
Location: Iowa

Post by ssand »

Ah, that would explain it then.
The copied code was using a number in the value. This new variable doesn't.

Thanks.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: mysql syntax question

Post by Benjamin »

Code: Select all

$sql = "UPDATE `inv_type` SET `inv_type_name`='$type_name', `inv_type_function`='$type_function'
						WHERE `inv_type_name`=$original_name";
It's also good practice to place backticks ` around table and field names as it can cause a query to fail in certain circumstances without them.
Post Reply