python72 Posted December 30, 2010 Share Posted December 30, 2010 I am trying to insert values stored within two dimensional array into mysql database but it does not work as I would expect it. The locations in mysql are defined as char length of 2. When I print_r the array it shows: Array( [0] => Array ( [0] => 04 [1] => 22 [2] => 27 [3] => 28 [4] => 39 [5] => 43 [6] => 47 )) but when I insert them into the mysql like this: Number1A='$MaxMillionsNumber[0][0]', Number1B='$MaxMillionsNumber[0][1]', Number1C='$MaxMillionsNumber[0][2]', Number1D='$MaxMillionsNumber[0][3]', Number1E='$MaxMillionsNumber[0][4]', Number1F='$MaxMillionsNumber[0][5]', Number1G='$MaxMillionsNumber[0][6]'; my values in mysql all show as Ar What am I doing wrong? Quote Link to comment https://forums.phpfreaks.com/topic/223043-trouble-inserting-array-variables-into-mysql/ Share on other sites More sharing options...
trq Posted December 30, 2010 Share Posted December 30, 2010 Variables are not interpolated within single quotes. Quote Link to comment https://forums.phpfreaks.com/topic/223043-trouble-inserting-array-variables-into-mysql/#findComment-1153182 Share on other sites More sharing options...
python72 Posted December 31, 2010 Author Share Posted December 31, 2010 What should I use then instead of single quotes in this case? When I just drop single quotes nothing is saved to mysql. Quote Link to comment https://forums.phpfreaks.com/topic/223043-trouble-inserting-array-variables-into-mysql/#findComment-1153207 Share on other sites More sharing options...
trq Posted December 31, 2010 Share Posted December 31, 2010 Can we see your actual query. The bit you have posted is probably a little out of context. Quote Link to comment https://forums.phpfreaks.com/topic/223043-trouble-inserting-array-variables-into-mysql/#findComment-1153210 Share on other sites More sharing options...
python72 Posted December 31, 2010 Author Share Posted December 31, 2010 mysql_query("INSERT INTO MaxMillionsNum SET Day='$weekday', DrawDate='$CompleteDrawDate', DateTime='$timestamp', Number1A='$MaxMillionsNumber[0][0]', Number1B='$MaxMillionsNumber[0][1]', Number1C='$MaxMillionsNumber[0][2]', Number1D='$MaxMillionsNumber[0][3]', Number1E='$MaxMillionsNumber[0][4]', Number1F='$MaxMillionsNumber[0][5]', Number1G='$MaxMillionsNumber[0][6]'; Quote Link to comment https://forums.phpfreaks.com/topic/223043-trouble-inserting-array-variables-into-mysql/#findComment-1153216 Share on other sites More sharing options...
BLaZuRE Posted December 31, 2010 Share Posted December 31, 2010 You're missing a final set of double quotes at the end, before the semicolon. Quote Link to comment https://forums.phpfreaks.com/topic/223043-trouble-inserting-array-variables-into-mysql/#findComment-1153224 Share on other sites More sharing options...
python72 Posted December 31, 2010 Author Share Posted December 31, 2010 I fixed that and it still does not work. Quote Link to comment https://forums.phpfreaks.com/topic/223043-trouble-inserting-array-variables-into-mysql/#findComment-1153226 Share on other sites More sharing options...
Pikachu2000 Posted December 31, 2010 Share Posted December 31, 2010 See what the query string actually holds: $query = "INSERT INTO MaxMillionsNum SET Day='$weekday', DrawDate='$CompleteDrawDate', DateTime='$timestamp', Number1A='$MaxMillionsNumber[0][0]', Number1B='$MaxMillionsNumber[0][1]', Number1C='$MaxMillionsNumber[0][2]', Number1D='$MaxMillionsNumber[0][3]', Number1E='$MaxMillionsNumber[0][4]', Number1F='$MaxMillionsNumber[0][5]', Number1G='$MaxMillionsNumber[0][6]'"; echo $query; Quote Link to comment https://forums.phpfreaks.com/topic/223043-trouble-inserting-array-variables-into-mysql/#findComment-1153229 Share on other sites More sharing options...
python72 Posted December 31, 2010 Author Share Posted December 31, 2010 Here is the result of $query INSERT INTO MaxMillionsNum SET Day='Fri', DrawDate='2010-12-24', DateTime='2010-12-30 19:02:31', Number1A='Array[0]', Number1B='Array[1]', Number1C='Array[2]', Number1D='Array[3]', Number1E='Array[4]', Number1F='Array[5]', Number1G='Array[6]' Now I know why I don't get the expected vaules saved in mysql but why this happens? Quote Link to comment https://forums.phpfreaks.com/topic/223043-trouble-inserting-array-variables-into-mysql/#findComment-1153241 Share on other sites More sharing options...
kenrbnsn Posted December 31, 2010 Share Posted December 31, 2010 Please post the results of <?php echo '<pre>' . print_r($MaxMillionsNumber,true) . '</pre>'; ?> I don't think your array holds what you think it holds. Ken Quote Link to comment https://forums.phpfreaks.com/topic/223043-trouble-inserting-array-variables-into-mysql/#findComment-1153245 Share on other sites More sharing options...
python72 Posted December 31, 2010 Author Share Posted December 31, 2010 Here is the result: Array ( [0] => Array ( [0] => 04 [1] => 22 [2] => 27 [3] => 28 [4] => 39 [5] => 43 [6] => 47 ) ) Quote Link to comment https://forums.phpfreaks.com/topic/223043-trouble-inserting-array-variables-into-mysql/#findComment-1153251 Share on other sites More sharing options...
kenrbnsn Posted December 31, 2010 Share Posted December 31, 2010 Try <?php $query = "INSERT INTO MaxMillionsNum SET Day='$weekday', DrawDate='$CompleteDrawDate', DateTime='$timestamp', Number1A='{$MaxMillionsNumber[0][0]}', Number1B='{$MaxMillionsNumber[0][1]}', Number1C='{$MaxMillionsNumber[0][2]}', Number1D='{$MaxMillionsNumber[0][3]}', Number1E='{$MaxMillionsNumber[0][4]}', Number1F='{$MaxMillionsNumber[0][5]}', Number1G='{$MaxMillionsNumber[0][6]}'"; echo $query; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/223043-trouble-inserting-array-variables-into-mysql/#findComment-1153258 Share on other sites More sharing options...
python72 Posted December 31, 2010 Author Share Posted December 31, 2010 kenrbnsn, This works, I guess my question is why I need these brackets and where I can find more info on when to use different ftypes of brackets and quotes? I have huge problem to decide when single or double quotes should be used as well as when different types of brackets should be used and can't seem to find this info in concise form so far. I just see them in examples so description comparing them would be helpful. Thanks a lot for all the help guys. Quote Link to comment https://forums.phpfreaks.com/topic/223043-trouble-inserting-array-variables-into-mysql/#findComment-1153378 Share on other sites More sharing options...
kenrbnsn Posted December 31, 2010 Share Posted December 31, 2010 Double quoted strings should be used when you want variables to be evaluated in the string or you want special characters like "\n" to work. You should put "{ }" around arrays in a double quoted string. Some examples. Compare the results of <?php $var = 'This is a test'; $arr = array('one'=>'Array entry'); echo '$var<br>'; echo "$var<br>"; echo 'The first entry in the array $arr is $arr["one"]<br>'; echo "The first entry in the array $arr is $arr['one']<br>"; // this will give you a syntax error echo "The first entry in the array \$arr is {$arr['one']}<br>"; //the "$" is quoted so PHP won't try to evaluated the variable ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/223043-trouble-inserting-array-variables-into-mysql/#findComment-1153386 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.