gazapo Posted February 20, 2011 Share Posted February 20, 2011 This is the weirdest thing ever. I've been programming php for years yet I have been pulling my hair out for hours over something that seems so simple! I have a table in my db named last_num_used with 2 fields: id, num which are both type int. It only has one row, so I don't need to use WHERE to update when I execute the command via php: $sql = "UPDATE last_num SET num='4'"; echo $sql; if (!mysql_query($sql)) {die('Error: ' . mysql_error());} everything works fine and I see the echoed command UPDATE last_num SET num='4' But, when I have a variable $var and try to update it, it will only update properly if $var=1 or $var=2. If $var equals a value greater than 2, then for some reason it updates the column so that num=1. Note the exact problem in example 3 below. Example 1: $var=1; $sql = "UPDATE last_num SET num='$var'"; echo $sql; if (!mysql_query($sql)) {die('Error: ' . mysql_error());} Result: echoed to screen: UPDATE last_num SET num='1' database updated and num=1 Example 2: $var=2; $sql = "UPDATE last_num SET num='$var'"; echo $sql; if (!mysql_query($sql)) {die('Error: ' . mysql_error());} Result: echoed to screen: UPDATE last_num SET num='2' database updated and num=2 Example 3: $var=3; $sql = "UPDATE last_num SET num='$var'"; echo $sql; if (!mysql_query($sql)) {die('Error: ' . mysql_error());} Result: echoed to screen: UPDATE last_num SET num='3' database updated and num=1 Quote Link to comment https://forums.phpfreaks.com/topic/228235-php-with-sql-update-command-not-working-for-numbers-2/ Share on other sites More sharing options...
jcbones Posted February 20, 2011 Share Posted February 20, 2011 So, what is the database dump of your table structure? Quote Link to comment https://forums.phpfreaks.com/topic/228235-php-with-sql-update-command-not-working-for-numbers-2/#findComment-1176977 Share on other sites More sharing options...
gazapo Posted February 20, 2011 Author Share Posted February 20, 2011 Wow, nevermind. I ended up not needing to use that function in that case anyway so I just dropped the table since it was only used for that. Thanks for taking a look. Quote Link to comment https://forums.phpfreaks.com/topic/228235-php-with-sql-update-command-not-working-for-numbers-2/#findComment-1177004 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.