morocco-iceberg Posted July 11, 2010 Share Posted July 11, 2010 I just need to know the correct syntax for using a variable variable within a mysql query. At the current time I have this: $sql = mysql_query("SELECT * FROM $table WHERE start = '{$'q'.$counter}' AND answer = '{$'q'.$counter}'") or die($error); Link to comment https://forums.phpfreaks.com/topic/207441-variable-variable-within-mysql-query/ Share on other sites More sharing options...
PFMaBiSmAd Posted July 11, 2010 Share Posted July 11, 2010 You do realize that if you used an array, the syntax would be easier to figure out and it would be quicker - If $q is an array so that the values are $q[0], $q[1], $q[2], you would use '{$q[$counter]}' to reference them in the query. Link to comment https://forums.phpfreaks.com/topic/207441-variable-variable-within-mysql-query/#findComment-1084533 Share on other sites More sharing options...
morocco-iceberg Posted July 11, 2010 Author Share Posted July 11, 2010 Oh, well that seems much more sensible (its 5:30am and I haven't slept). Thanks! Link to comment https://forums.phpfreaks.com/topic/207441-variable-variable-within-mysql-query/#findComment-1084534 Share on other sites More sharing options...
PFMaBiSmAd Posted July 11, 2010 Share Posted July 11, 2010 Not to mention that setting or referencing an array variable is 3x faster than using a variable-variable. Also with arrays, you can use array functions, such as foreach() and count() so that you don't need to keep track of how many of or what the names of the variable-variables are. Link to comment https://forums.phpfreaks.com/topic/207441-variable-variable-within-mysql-query/#findComment-1084535 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.