Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies




5:08 pm on Feb 2, 2012 (gmt 0)

5+ Year Member

Hi folks,

I've been looking up foreach in the manual but I can't quite understand why the below doesn't work?

$whosonline = "SELECT log_activecheck FROM members_log WHERE log_isonline = 'y'"; // there are 3 members set to be online

$result = mysql_query($whosonline);
$row = mysql_fetch_array($result);

$numrows = mysql_num_rows($result);
echo $numrows . "<br>"; // has it got the right ammount? Yes, 3

foreach ($row as $activecheck) {

echo $activecheck . "<br>"; }

What am I failing to grasp here? That's just how it looks in the manual except they predfine the array with something like $row = array(1, 2, 3) instead of my 'fetch_array'.

The results I get are '3' from the row count which is right, then for the activecheck which displays a timestamp it displays two lines both containing the timestamp of the first db entry to meet the selection check of 'y'


5:22 pm on Feb 2, 2012 (gmt 0)

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member

$numrows = mysql_num_rows($result);

What am I failing to grasp here?

You create an "object" and store that instance of the object in the variable $result.

The function mysql_num_rows() returns a scalar. A scalar is a flat single value, whether it be a string, integer, float, whatever, it is not a list like an array.

foreach ($row as $activecheck) {

So your foreach goes through the loop ONCE because there's only one value in $row (at this point,) 3. If you were to do this,

for ($i=1;$i<=$row;$i++) {
echo "$i \n";

It would indeed echo three times, but . . .$row is still 3. :-) To extract values from $result, you need to use one of the fetch methods.

Before you start, don't use the $rows as a counter to step through. It's not needed. Use a while on result.

while ($row=mysql_fetch_array($result)) {
// NOW $row is an array - but it's an array of the FIELDS you selected
$len = count($rows);
echo "<p>";
for ($i=0;$i<$len;$i++) { // Remembering arrays start at index 0 (zero)
echo " i: $i " . $row[$i] . " ";
echo "<p>\n";


7:40 pm on Feb 2, 2012 (gmt 0)

5+ Year Member

Wow, cool, thanks! :)

My actual end objective is to write a little possible cron job scrip to run every 20 minutes and check site members last activity time against '20 minutes ago' and mark them as offline where appropriate. In the aid of creating a little who's online feature.

I've put together this but all it does is set everyone to 'n' or offline as soon as the first runs out of time, despite others still being within the 20 mins.

$whosonline = "SELECT log_activecheck FROM members_log WHERE log_isonline = 'y'";
$result = mysql_query($whosonline);

$twmincheck = strtotime ( " -20 minutes " ); // the 20 mins ago maths in english, nice

if ($row = mysql_fetch_array($result)) {

do {
$activecheck1 = $row['log_activecheck']; // sql timestamp
$activecheck = strtotime($activecheck1); // php safe now

if ($twmincheck > $activecheck) {
$nowoffline = "UPDATE members_log";
$nowoffline.= " SET log_isonline = 'n'"; // outside 20 mins they're now offline
} // end if

} // end do

while ($row = mysql_fetch_array($result));

}; //end if

I've been trying to bash this together in php but am not sure if there's a method it could just be done in some kind of query. I read that sql can do some handy things with dates if they're stored as datetime as opposed to php's crazy date numbers which you can store as an int, but what those handy things were wasn't really made clear as it was on the topic of converting betwen the two with strtotime() so I could do my 20 minute check easily.


11:27 am on Feb 3, 2012 (gmt 0)

5+ Year Member

Talk about a mammoth waste of time! There's always a sleek and simple answer somewhere isn't there, and sods law it's usually a one liner :D
There's many more handy things to sql than just insert, select, update and equals! I was expecting some complcated IS BETWEEN date style thing but noooo

$nowoffline = "UPDATE members_log SET log_isonline = 'n' WHERE log_activecheck < '" . $twminsago . "' AND log_isonline = 'y'";

magic :D

Possibly a mite off php topic but I wanted to post my answer...


5:57 pm on Feb 3, 2012 (gmt 0)

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member

Nothing in which you learn something new is a waste. Here's another one. :-)


I'm sure you arrived at this via the PHP date/time functions. What happens if your mysql server's time differs from your PHP server's time? It happens.

Besides that, there is already date/time function directly in mysql [dev.mysql.com]. Have a look at this instead.

$nowoffline = "UPDATE members_log SET log_isonline = 'n' WHERE log_activecheck < date_sub(now(), interval 2 minute) AND log_isonline = 'y'";

This of course requires the field is a valid datetime format, either by timestamp or YYYY-MM-DD HH:MM:SS.

Featured Threads

Hot Threads This Week

Hot Threads This Month