Jump to content

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


surochek

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.