dmhall0 Posted March 15, 2012 Share Posted March 15, 2012 I have a MySQL query that pulls multiple columns with many rows. I want to break each column into its own array. Here is what I have to this point but doesn't seem to be working. Is this the right direction or is there something easier? Thanks! $query = "SELECT name, address, city, state FROM customers WHERE sku = '12345'"; $result = mysqli_query($dbc, $query) or die(); $name = array(); $address = array(); $city = array(); state = array(); while ($data = mysqli_fetch_assoc($result)) { $name = $data['name']; $address = $data['address']; $city = $data['city']; $state = $data['state']; } Quote Link to comment https://forums.phpfreaks.com/topic/258981-divide-multidimensional-array-into-many-arrays/ Share on other sites More sharing options...
Muddy_Funster Posted March 15, 2012 Share Posted March 15, 2012 neerly there: $query = "SELECT name, address, city, state FROM customers WHERE sku = '12345'"; $result = mysqli_query($dbc, $query) or die(); $name = array(); $address = array(); $city = array(); state = array(); while ($data = mysqli_fetch_assoc($result)) { $name[] = $data['name']; $address[] = $data['address']; $city[] = $data['city']; $state[] = $data['state']; } Quote Link to comment https://forums.phpfreaks.com/topic/258981-divide-multidimensional-array-into-many-arrays/#findComment-1327608 Share on other sites More sharing options...
AyKay47 Posted March 15, 2012 Share Posted March 15, 2012 The only thing you are missing is the correct syntax for pushing values onto an array using the [] operator. $name[] = $data['name']; Also, it's a good idea to instead have die(mysqli_error()); to be able to properly debug the query upon failure. Edit: muddy beat me to it. Quote Link to comment https://forums.phpfreaks.com/topic/258981-divide-multidimensional-array-into-many-arrays/#findComment-1327610 Share on other sites More sharing options...
dmhall0 Posted March 15, 2012 Author Share Posted March 15, 2012 Devil in the details! Thanks guys! Quote Link to comment https://forums.phpfreaks.com/topic/258981-divide-multidimensional-array-into-many-arrays/#findComment-1327611 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.