Drongo_III Posted March 2, 2012 Share Posted March 2, 2012 Hi Guys Clearly I still have lots to learn... I am trying to insert (using mysqli) multi-dimensional array values into the database, but when i do the value that gets stored ends up as "$Array[1][1]". Here's the test query i am running: $sql = "INSERT INTO teams (company) VALUES ('$myarray[1][1]')"; $mysqli->query($sql); So do i need to break the data out of the multidimensional array for the insert? Or is there some syntax thing i've missed? Or worse still do i actually need to do: $value1 = $myarray[0][0]; Any help would be very much appreciated! Drongo Quote Link to comment https://forums.phpfreaks.com/topic/258141-inserting-multidimensional-array-value-into-mysql/ Share on other sites More sharing options...
freelance84 Posted March 2, 2012 Share Posted March 2, 2012 This should work.. $sql = "INSERT INTO teams (company) VALUES ('".$myarray[1][1]."')"; $mysqli->query($sql); Quote Link to comment https://forums.phpfreaks.com/topic/258141-inserting-multidimensional-array-value-into-mysql/#findComment-1323250 Share on other sites More sharing options...
Drongo_III Posted March 2, 2012 Author Share Posted March 2, 2012 That worked a treat! Thank you. Err... mind explaining to me why you wrapped it in double quotes and then concatenated? Is this some way of make it interpret the next level down in the array? This should work.. $sql = "INSERT INTO teams (company) VALUES ('".$myarray[1][1]."')"; $mysqli->query($sql); Quote Link to comment https://forums.phpfreaks.com/topic/258141-inserting-multidimensional-array-value-into-mysql/#findComment-1323252 Share on other sites More sharing options...
freelance84 Posted March 2, 2012 Share Posted March 2, 2012 To be honest I don't know the actual reason. You can place a single postion into a "" string: $a[] = 'ss'; echo <<<_END $a[0] _END; But when you chuck in another dimension with the extra set of square brackets, php trips up. So as your query in $sql is essentailly a string, by breaking the string and joining it to the array pos, then joining the rest of the query to the end the $sql... $sql then becomes the string you wanted it to be in the first place. However i'm pretty sure there is a better explanation.. and I would actually like to know it too... i notice you have Adam reading this thread.. hopefully he will be able to give a better explanation to the one i gave (you can actually write less code there by placing the query directly into the mysqli: $mysqli->query( "INSERT INTO teams (company) VALUES ('".$myarray[1][1]."')" ); Quote Link to comment https://forums.phpfreaks.com/topic/258141-inserting-multidimensional-array-value-into-mysql/#findComment-1323274 Share on other sites More sharing options...
Drongo_III Posted March 2, 2012 Author Share Posted March 2, 2012 Well that sounds like a pretty decent explanation to me I was more concerned there was some fundamental syntax thing i had completely overlooked. It's good to know how to make it work though so thank you very much! To be honest I don't know the actual reason. You can place a single postion into a "" string: $a[] = 'ss'; echo <<<_END $a[0] _END; But when you chuck in another dimension with the extra set of square brackets, php trips up. So as your query in $sql is essentailly a string, by breaking the string and joining it to the array pos, then joining the rest of the query to the end the $sql... $sql then becomes the string you wanted it to be in the first place. However i'm pretty sure there is a better explanation.. and I would actually like to know it too... i notice you have Adam reading this thread.. hopefully he will be able to give a better explanation to the one i gave (you can actually write less code there by placing the query directly into the mysqli: $mysqli->query( "INSERT INTO teams (company) VALUES ('".$myarray[1][1]."')" ); Quote Link to comment https://forums.phpfreaks.com/topic/258141-inserting-multidimensional-array-value-into-mysql/#findComment-1323281 Share on other sites More sharing options...
marcbraulio Posted March 2, 2012 Share Posted March 2, 2012 Err... mind explaining to me why you wrapped it in double quotes and then concatenated? That is actually a very good question, I am tempted to say it has something to do with greedy token, but that wouldn't make any sense. If anyone knows please share! But alternatively you can use braces like so (it's more readable in my opinion): "INSERT INTO teams (company) VALUES ('{$myArray[0][3]}')" Or you can just asign the array value before hand and insert a regular string like so: $myValue = $myArray[0][2]; "INSERT INTO teams (company) VALUES ('$myValue')" Quote Link to comment https://forums.phpfreaks.com/topic/258141-inserting-multidimensional-array-value-into-mysql/#findComment-1323286 Share on other sites More sharing options...
Drongo_III Posted March 2, 2012 Author Share Posted March 2, 2012 Ahhh....i was trying to use braces but i wasn't wrapping the whole variable. haha that makes me wonder about something else... Do curly braces tell php to interpret that part first? Err... mind explaining to me why you wrapped it in double quotes and then concatenated? That is actually a very good question, I am tempted to say it has something to do with greedy token, but that wouldn't make any sense. If anyone knows please share! But alternatively you can use braces like so (it's more readable in my opinion): "INSERT INTO 2012hitting (hr) VALUES ('{$myArray[0][3]}')" Or you can just asign the array value before hand and insert a regular string like so: $myValue = $myArray[0][2]; $sql = "INSERT INTO 2012hitting (hr) VALUES ('$myValue')"; Quote Link to comment https://forums.phpfreaks.com/topic/258141-inserting-multidimensional-array-value-into-mysql/#findComment-1323289 Share on other sites More sharing options...
Pikachu2000 Posted March 2, 2012 Share Posted March 2, 2012 When you're dealing with a multidimensional array, and need to use one of the indices deeper than the first dimension in a quoted string (like a query string), it seems that you need to use complex notation, with the curly braces, or concatenate it into the string. Otherwise, you can only reference elements in the first dimension of the array. I took a quick look in the manual, but couldn't find any explanation, but that doesn't mean it isn't there somewhere. Quote Link to comment https://forums.phpfreaks.com/topic/258141-inserting-multidimensional-array-value-into-mysql/#findComment-1323295 Share on other sites More sharing options...
freelance84 Posted March 2, 2012 Share Posted March 2, 2012 Oh yea you can use curly's too... I don't know which is preffered by the guru's... and also i don't know which is more optimum.. I just prefer to break and join as I find it easier to read, loads of curlies makes it look like some old english text from a dickens novel in my opinion haha Quote Link to comment https://forums.phpfreaks.com/topic/258141-inserting-multidimensional-array-value-into-mysql/#findComment-1323297 Share on other sites More sharing options...
Pikachu2000 Posted March 2, 2012 Share Posted March 2, 2012 I find it easier to read with curly braces, and less likely to have typos. Quote Link to comment https://forums.phpfreaks.com/topic/258141-inserting-multidimensional-array-value-into-mysql/#findComment-1323303 Share on other sites More sharing options...
Drongo_III Posted March 2, 2012 Author Share Posted March 2, 2012 well i feel like i've learned something today I think i'm down with the curly braces club freelancer...seems easier to read to me. So you're out there on concatenation island on your own i'm afraid hehe. Thanks for all that chaps. Has helped a lot! Quote Link to comment https://forums.phpfreaks.com/topic/258141-inserting-multidimensional-array-value-into-mysql/#findComment-1323304 Share on other sites More sharing options...
freelance84 Posted March 2, 2012 Share Posted March 2, 2012 haha.. ah well Quote Link to comment https://forums.phpfreaks.com/topic/258141-inserting-multidimensional-array-value-into-mysql/#findComment-1323306 Share on other sites More sharing options...
marcbraulio Posted March 2, 2012 Share Posted March 2, 2012 Do curly braces tell php to interpret that part first? Lol, I guess some one will just have to call up Rasmus Lerdorf for a full explanation. But also be aware of the use of curly braces in variables in order to separate it for from the rest of the text. For instance: $dona = "connec"; $dona = "educa"; $dona = "presenta"; $dona = "evolu" // normally you would do: $myVar = 'The word' . $dona . 'tion could have a different beginning but always ends the same way.'; // but alternatively you can do (greedy token parsing): $myVar = "The word {dona}tion could have a different beginning but always ends the same way."; So in order to separate $dona from "tion" you use the curly braces, thus avoiding php from reading the string as $donation. Figured I would point this out for informational purposes considering it is probably closely related to the use of concatenation and curly braces in the array. Quote Link to comment https://forums.phpfreaks.com/topic/258141-inserting-multidimensional-array-value-into-mysql/#findComment-1323312 Share on other sites More sharing options...
Drongo_III Posted March 2, 2012 Author Share Posted March 2, 2012 Cheers marc that;s good to know. I can see keeping curly braces in mind can probably come in very handy! Do curly braces tell php to interpret that part first? Lol, I guess some one will just have to call up Rasmus Lerdorf for a full explanation. But also be aware of the use of curly braces in variables in order to separate it for from the rest of the text. For instance: $begin = "connec"; $dona = "educa"; $dona = "presenta"; $dona = "evolu" // normally you would do: $myVar = 'The word' . $dona . 'tion could have a different beginning but always ends the same way.'; // but alternatively you can do (greedy token parsing): $myVar = "The word {dona}tion could have a different beginning but always ends the same way."; So in order to separate $dona from "tion" you use the curly braces, thus avoiding php from reading the string as $donation. Figured I would point this out for informational purposes considering it is probably closely related to the use of concatenation and curly braces in the array. Quote Link to comment https://forums.phpfreaks.com/topic/258141-inserting-multidimensional-array-value-into-mysql/#findComment-1323316 Share on other sites More sharing options...
kicken Posted March 3, 2012 Share Posted March 3, 2012 String Parsing If a dollar sign ($) is encountered, the parser will greedily take as many tokens as possible to form a valid variable name. [ and ] are not considered valid variable name characters, so PHP stop at that point. It make a special case though where if the next character is a [, it will properly interpret it. This only works for one level though. For multiple levels, you have to explicitly tell PHP what the variable name is using the complex notation. This is also why PHP cannot handle quoted keys (such as $arr['hi']) in a string without the complex notation. After the [ it scans for more valid variable characters and stop at first invalid one. ' or " are not valid so you end up requesting the variable $arr[ which causes PHP to error. Quote Link to comment https://forums.phpfreaks.com/topic/258141-inserting-multidimensional-array-value-into-mysql/#findComment-1323341 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.