msmyth23 Posted June 8, 2011 Share Posted June 8, 2011 Hey, I'm having trouble getting some PHP arrays from my database. This isn't exactly what is in my database, but this is a good example of what I'm trying to implement. My database table (called 'tablename') looks like this (minus keys and ids and whatnot): +----------------+------------------+ | users | pagename | +----------------+------------------+ | user1 | page1 | | user1 | page2 | | user2 | page3 | | user2 | page4 | +-----------------+-----------------+ And my PHP looks sort of like this: $user_array = mysql_fetch_array(mysql_query("SELECT pagename FROM tablename WHERE user = 'user1'"), MYSQL_NUM) or die(mysql_error()); The output I'm looking for is like this: Array( [0] => 'page1', [1] => 'page2') But I'm just getting the index 0 in the array: Array([0] => 'page1') Do you know anyway I can solve this to get all of the pagenames that have the same user into one array? It's probably really simple but I've been thinking for hours and can't get it. Thanks for your help, Cheers! Michael Quote Link to comment https://forums.phpfreaks.com/topic/238801-drawing-multiple-database-values-into-single-array-using-where-clause/ Share on other sites More sharing options...
wildteen88 Posted June 8, 2011 Share Posted June 8, 2011 The mysql_fetch_* functions return one row at a time. As your query will return more than two rows you'll need to use these functions with a while loop to get all the results $result = mysql_query("SELECT pagename FROM tablename WHERE user = 'user1'") or die(mysql_error(); // definee userPages as an array. We'll add the userpages to this array latter $userPages = array(); // loop through the results, one row at a time while($row = mysql_fetch_assoc($result)) { // add the pagename to userPages array $userPages[] = $row['pagename']; } // output the the userPages array echo '<pre>' . print_r($userPages, true) . '</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/238801-drawing-multiple-database-values-into-single-array-using-where-clause/#findComment-1227068 Share on other sites More sharing options...
msmyth23 Posted June 8, 2011 Author Share Posted June 8, 2011 The mysql_fetch_* functions return one row at a time. As your query will return more than two rows you'll need to use these functions with a while loop to get all the results $result = mysql_query("SELECT pagename FROM tablename WHERE user = 'user1'") or die(mysql_error(); // definee userPages as an array. We'll add the userpages to this array latter $userPages = array(); // loop through the results, one row at a time while($row = mysql_fetch_assoc($result)) { // add the pagename to userPages array $userPages[] = $row['pagename']; } // output the the userPages array echo '<pre>' . print_r($userPages, true) . '</pre>'; It worked to perfection I knew it had to be a lot simpler than I was making it. Thanks a bunch. Cheers, Michael Quote Link to comment https://forums.phpfreaks.com/topic/238801-drawing-multiple-database-values-into-single-array-using-where-clause/#findComment-1227070 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.