briant Posted February 2, 2008 Share Posted February 2, 2008 I recently discovered arrays... when I say recent, I mean today, haha... I've looked around and its incredible hard to find a tutorial on it. Let's say this is my array: $names = array("Brian","Billy","Bob","Joe"); How am I suppose to update my array? mysql_query("UPDATE site_table SET friends = 'what do i put in here' WHERE user_id = 2"); Also, if I want to add another name to the array, how am I to do that? Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/89108-solved-inserting-and-updating-arrays/ Share on other sites More sharing options...
pocobueno1388 Posted February 2, 2008 Share Posted February 2, 2008 Here is how you would add a name later on in the script <?php $names = array("Brian","Billy","Bob","Joe"); //Add a name $names[] = "Cindy"; ?> As for your query question, I would need to see how you had your database table setup to know how to insert the information. Quote Link to comment https://forums.phpfreaks.com/topic/89108-solved-inserting-and-updating-arrays/#findComment-456372 Share on other sites More sharing options...
briant Posted February 2, 2008 Author Share Posted February 2, 2008 I set the data type to text for that "friends" field. Isn't the array suppose to appear in one field? Quote Link to comment https://forums.phpfreaks.com/topic/89108-solved-inserting-and-updating-arrays/#findComment-456375 Share on other sites More sharing options...
pocobueno1388 Posted February 2, 2008 Share Posted February 2, 2008 So you want it to appear in the database in a text field as one line like so? Brian Billy Bob Joe If thats how you want it, then you would do this: <?php $names = array("Brian","Billy","Bob","Joe"); $query = "UPDATE site_table SET friends = '" . implode($names, " ") . "' WHERE user_id = 2" $result = mysql_query($query)or die(mysql_error()); ?> Quote Link to comment https://forums.phpfreaks.com/topic/89108-solved-inserting-and-updating-arrays/#findComment-456376 Share on other sites More sharing options...
briant Posted February 2, 2008 Author Share Posted February 2, 2008 Thanks pocobueno1388 for your rapid responses. OK, I think I might not have understood the use of arrays... Am I still able to call upon the the friends field as an array? like $friends[3]? And it would show Bob. Quote Link to comment https://forums.phpfreaks.com/topic/89108-solved-inserting-and-updating-arrays/#findComment-456382 Share on other sites More sharing options...
pocobueno1388 Posted February 2, 2008 Share Posted February 2, 2008 Thanks pocobueno1388 for your rapid responses. OK, I think I might not have understood the use of arrays... Am I still able to call upon the the friends field as an array? like $friends[3]? And it would show Bob. Yes, you will still be able to call which name you want from the array by using the key like you did. But Bob would be $friend[2] because the counter starts at 0...here is an example. <?php $names = array("Brian","Billy","Bob","Joe"); 0 1 2 3 Does that make sense? Quote Link to comment https://forums.phpfreaks.com/topic/89108-solved-inserting-and-updating-arrays/#findComment-456390 Share on other sites More sharing options...
Stooney Posted February 2, 2008 Share Posted February 2, 2008 In the example above, implode() just condenses the array into a string, separating each element with a space in that instance. Just in case that was unclear Quote Link to comment https://forums.phpfreaks.com/topic/89108-solved-inserting-and-updating-arrays/#findComment-456399 Share on other sites More sharing options...
briant Posted February 2, 2008 Author Share Posted February 2, 2008 Thanks I knew it was 2, I don't know why I put 3. Anyways, what I meant to say was, if I SELECT friends FROM site_table WHERE user_id = 2; Will the Friends from the database still be an array? or just a string? Quote Link to comment https://forums.phpfreaks.com/topic/89108-solved-inserting-and-updating-arrays/#findComment-456400 Share on other sites More sharing options...
Stooney Posted February 2, 2008 Share Posted February 2, 2008 It's a string when it comes from the database. You can't store an actual array in a database, only the proper information for a script to rebuild the array after the query. Quote Link to comment https://forums.phpfreaks.com/topic/89108-solved-inserting-and-updating-arrays/#findComment-456401 Share on other sites More sharing options...
pocobueno1388 Posted February 2, 2008 Share Posted February 2, 2008 They would be in a string. Although you can easily change it back to an array if you wanted like this: <?php //we will say this is the variable from the database $friends = $row['friends']; //put them back into an array $friends = explode(" ", $friends); ?> Quote Link to comment https://forums.phpfreaks.com/topic/89108-solved-inserting-and-updating-arrays/#findComment-456402 Share on other sites More sharing options...
briant Posted February 2, 2008 Author Share Posted February 2, 2008 Wow, awesome pocobueno1388... you are very knowledgeable. One last thing and I'll leave you alone. Let's say instead of a " " space, since I might have a name that includes the last name "Bob Barker". I want all my array stored in the database with brackets. For example: [brian][billy][bob Barker][Joe][Cindy] How am I suppose to explode that into an array? Maybe it's this? $friends = explode("[", $friends, "]"); Quote Link to comment https://forums.phpfreaks.com/topic/89108-solved-inserting-and-updating-arrays/#findComment-456409 Share on other sites More sharing options...
pocobueno1388 Posted February 3, 2008 Share Posted February 3, 2008 Instead, why don't you just separate them by commas? That way you don't have to deal with the pain of selecting names in between two characters. Brian, Billy, Bob, etc Quote Link to comment https://forums.phpfreaks.com/topic/89108-solved-inserting-and-updating-arrays/#findComment-456411 Share on other sites More sharing options...
briant Posted February 3, 2008 Author Share Posted February 3, 2008 Hmm, maybe, just maybe I could, haha... thanks a lot for your help. I appreciate your time. Quote Link to comment https://forums.phpfreaks.com/topic/89108-solved-inserting-and-updating-arrays/#findComment-456413 Share on other sites More sharing options...
pocobueno1388 Posted February 3, 2008 Share Posted February 3, 2008 No problem Quote Link to comment https://forums.phpfreaks.com/topic/89108-solved-inserting-and-updating-arrays/#findComment-456414 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.