Cobby Posted October 10, 2007 Share Posted October 10, 2007 Hi, I have a user table in the DB, for example: +---------+----------------+-------------------+ | uid | username | firstname | |----------+----------------+------------------+ | 1 | cobby | Andrew | | 2 | admini | test-account | +---------+-----------------+------------------+ If, in phpMyAdmin, I query: SELECT `username` FROM `users`; It will return results correctly (cobby and admini, respectively). But if I make a simple PHP script, such as: <?php mysql_connect('localhost', 'cobby', 'dbpass'); mysql_select_db('testdb'); $query = mysql_query('SELECT `username` FROM `users`'); print_r(mysql_fetch_assoc($query)); ?> It only returns: Array ( [username] => admini ) Why does it only return the second result? I have a feeling I'm doing something really silly, but I looked over this test script and its got me stumped. Cheers, Cobby Quote Link to comment https://forums.phpfreaks.com/topic/72592-mysql-query-not-returning-all-results/ Share on other sites More sharing options...
adam291086 Posted October 10, 2007 Share Posted October 10, 2007 try this. I am new to php but i have done this before and it works. $query = mysql_query("SELECT username FROM users"); while($row = mysql_fetch_array($query)) { echo $row['username'] echo "<br />"; } Quote Link to comment https://forums.phpfreaks.com/topic/72592-mysql-query-not-returning-all-results/#findComment-366062 Share on other sites More sharing options...
Cobby Posted October 10, 2007 Author Share Posted October 10, 2007 Ah, I see what is going on now, its trying to return two identical indexes, and because admini is the last row, its value are set into the array. It was something silly, lol :-\ Quote Link to comment https://forums.phpfreaks.com/topic/72592-mysql-query-not-returning-all-results/#findComment-366065 Share on other sites More sharing options...
adam291086 Posted October 10, 2007 Share Posted October 10, 2007 Yep, so just add the while loop to say while there is info in the query results echo the username Quote Link to comment https://forums.phpfreaks.com/topic/72592-mysql-query-not-returning-all-results/#findComment-366066 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.