surochek Posted May 18, 2007 Share Posted May 18, 2007 The database is OK. The query works. But for the rest of my script to function as it should, this part must return all the rows corresponding to the query. I only get the first one (should be 2 from my test database). What am I doing wrong? ??? <?php $query = "SELECT * FROM user WHERE DATE_ADD(regdate, INTERVAL 5 day) < CURDATE()"; $result=mysql_query($query)or die (mysql_error()); $num=mysql_num_rows($result); $user_array = mysql_fetch_array($result); for ($i=0; $i<$num; $i++) { echo $user_array["username"]."<br>"; } ?> Thank you from a very frustrated learner. Quote Link to comment https://forums.phpfreaks.com/topic/51987-solved-why-does-this-return-only-the-first-row-in-the-database/ Share on other sites More sharing options...
Orio Posted May 18, 2007 Share Posted May 18, 2007 You are using mysql_fetch_array() in the wrong way. <?php $query = "SELECT * FROM user WHERE DATE_ADD(regdate, INTERVAL 5 day) < CURDATE()"; $result=mysql_query($query)or die (mysql_error()); while ($user_array = mysql_fetch_array($result)) { echo $user_array["username"]." "; } ?> Orio. Quote Link to comment https://forums.phpfreaks.com/topic/51987-solved-why-does-this-return-only-the-first-row-in-the-database/#findComment-256250 Share on other sites More sharing options...
Barand Posted May 18, 2007 Share Posted May 18, 2007 mysql_fetch_array fetch a row at a time, putting the fields of that row into an array Quote Link to comment https://forums.phpfreaks.com/topic/51987-solved-why-does-this-return-only-the-first-row-in-the-database/#findComment-256251 Share on other sites More sharing options...
SoulAssassin Posted May 18, 2007 Share Posted May 18, 2007 You might also want to try adding MYSQL_ASSOC in there like this: <? while ($user_array = mysql_fetch_array($result, MYSQL_ASSOC)) { ?> Quote Link to comment https://forums.phpfreaks.com/topic/51987-solved-why-does-this-return-only-the-first-row-in-the-database/#findComment-256257 Share on other sites More sharing options...
surochek Posted May 18, 2007 Author Share Posted May 18, 2007 It works! Thank you! (Feeling dumb ) Why? Does mysql_fetch_array stop itself, so to speak, therefore a while loop doesn't turn into an infinite loop? Surochek. Quote Link to comment https://forums.phpfreaks.com/topic/51987-solved-why-does-this-return-only-the-first-row-in-the-database/#findComment-256326 Share on other sites More sharing options...
Orio Posted May 18, 2007 Share Posted May 18, 2007 From the manual: Returns an array that corresponds to the fetched row and moves the internal data pointer ahead. That means that it grabs a row, moves the internal pointer ahead. If the internal pointer reached the end ( => The last row was grabbed in the last run), it returns false. See more info and examples in the manual. Orio. Quote Link to comment https://forums.phpfreaks.com/topic/51987-solved-why-does-this-return-only-the-first-row-in-the-database/#findComment-256332 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.