Take the 2-minute tour ×

Im using mysql with PHP, I was just wondering if this query,

$query = "UPDATE tblName SET field='$fieldValue' WHERE field2='$fieldValue2'"
would cause an Out of Memory Error in mysql. Will this query,
$query = "UPDATE tblName SET field='".$fieldValue."' WHERE field2='".$fieldValue2."'"
consume less memory than the previous one?

Im getting this error: Out of memory (Needed nnnnnnn bytes) and its pointing to the query with the same format as the first one above.


share|improve this question
Not an answer, but just a good advice: Never use $var = "Text $var text";. That's so utterly bad and horribly programming style my eyes keep on popping out every time I see it. It is safer and even easier to maintain code that is always properly escaped. Just because you can do it doesn't mean it's good style. So for the love of god: Escape your variables! ALWAYS! –  Florian Peschka Nov 11 '09 at 12:18
oh.. thanks for the programming tip. ill try to remember it next time. –  uji Nov 11 '09 at 12:20
ApoY2k, you're massively over-exaggerating. Sure, escape user input, but saying $var = "Text $var text"; is terrible is just wrong. –  DisgruntledGoat Nov 11 '09 at 15:24
add comment

4 Answers

up vote 1 down vote accepted

As far as MySQL is concerned, the two strings are the same.

How big are these values in $fieldValue and $fieldValue2? If they're multiple megabytes, attempting to allocate space for the $query variable may be exceeding your PHP memory limit - perhaps you need to up it if you're working with large data.

share|improve this answer
The values for $fieldValue and $fieldValue2 are not big. Not yet anyway. Anyways thanks, ill try increasing the limit –  uji Nov 11 '09 at 12:23
add comment

If you code like that, this will happen. Don´t code like that, please.

share|improve this answer
The code posted doesn't indicate where the field values are coming from - it's quite possible that the values in the variables have already been properly pre-processed for validation+escaping. –  Amber Nov 11 '09 at 12:26
Could be, but not likely. Call it a hunch ^^ –  anddoutoi Nov 11 '09 at 12:39
@Dav No, even in that case you are wrong. Use prepared statements. Whatever you do, just do not use string concatenation. –  shylent Nov 11 '09 at 13:04
add comment

Both strings are the same, one uses inline interpolation, the other uses string concatenation. I think your problem has to be found somewhere else.

share|improve this answer
add comment

Well the out of memory error is probably caused by the fact that the values in those variables are simply too long. If that is the case you should look into prepared statements as those can handle MUCH bigger values than simple queries.

share|improve this answer
add comment

Your Answer


