Mistral 🤖 Posted November 6, 2013 Share Posted November 6, 2013 I realize this straddles the line into MySQL...I am hoping someone can show me another way... I have the following code that builds a sql query string: $sql='UPDATE mytable SET user_class = $class_list WHERE user_id = $userid '; When I "echo" it out for troubleshooting it looks right. However...I have a situation where the $class_list string translates to an empty string "". This appears to be throwing MySQL into a syntax error: It seems to think the column I am referencing in the WHERE is the $userid instead of user_id (which IS correct) UPDATE e107_test SET user_class = $class_list WHERE user_id = $userid Could not update class: Unknown column '$userid' in 'where clause' When I do this: $sql='UPDATE mytable SET user_class = "$class_list" WHERE user_id = "$userid" '; This is the echo result - UPDATE mytable SET user_class = "$class_list" WHERE user_id = "$userid" It no longer complains about an Unknown column but it also does not update any records. I have the following to help troubleshoot: $sqlresult = mysql_query( $sql1, $conn );if(! $sqlresult ){ die('Could not update class: ' . mysql_error()); exit;}else{echo 'Changed 1 record';echo '<br>';} //This is the end of the $sql1 query Here is what I get: UPDATE mytable SET user_class = "$class_list" WHERE user_id = "$userid" Changed 1 record I get the "Changed 1 record" output for every record in table but refreshing the table in phpMyAdmin shows that no data has changed. I know I'm doing something stupid.... ?? Quote Link to comment https://forums.phpfreaks.com/topic/283660-mysql-query-string-help/ Share on other sites More sharing options...
Solution Mistral 🤖 Posted November 6, 2013 Solution Share Posted November 6, 2013 you are on the right track as far as wrapping it in quotes. However, your overall quotes for the query are single quotes. php variables are not parsed inside single quotes. Make your outer quotes double and your inner quotes single, or else break out of the quotes and concatenate Quote Link to comment https://forums.phpfreaks.com/topic/283660-mysql-query-string-help/#findComment-1457230 Share on other sites More sharing options...
Mistral 🤖 Posted November 6, 2013 Author Share Posted November 6, 2013 Reversing the quotes did it! I *thought* I had tried that previously but obviously bungled it or did not hit upon that variation. I knew I was close (hoped, anyway!). Many thanks!! Quote Link to comment https://forums.phpfreaks.com/topic/283660-mysql-query-string-help/#findComment-1457236 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.