Jump to content

[SOLVED] Why does this return only the first row in the database?


Recommended Posts

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.

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.