Jump to content

Query, looping with while, simply question


PC Nerd

Recommended Posts

um, i get that its expecting an ";" on the while loop line,

 

do{

$Login = mysqli_fetch_array($Login_Query);

$Latest_time = $Login['Logged_In'];

}

while($Login = mysqli_fetch_array($Login_Query)) {

if($Latest_time < $Login['Logged_In']) {

$Login_ID = $Login['Login_ID'];

}

}

 

 

there is it meant to go???

 

ive highlited the line where the ";" is expected.

 

 

thanks in advance, PC Nerd

 

 

Link to comment
Share on other sites

I think PHP is getting very confused with how you wrote the loop. Try this instead:

<?php
while($Login = mysqli_fetch_array($Login_Query)) {
   $Latest_time = $Login['Logged_In'];
   if($Latest_time < $Login['Logged_In'])
      $Login_ID = $Login['Login_ID'];
}?>

 

But looking at the above, the logic doesn't make any sense. What are you trying to accomplish?

 

Ken

Link to comment
Share on other sites

What format is the login date/time stored in? If it's not a UNIX time stamp, then a simple comparison won't work.

 

Try this code (it assumes the login date/time is stored as "YYYY-MM-DD HH:MM:00")

<?php
$current_time = time(); // get the current time as a UNIX Timestamp
$most_recent = 0; // initialize to earliest possible login time
while($Login = mysqli_fetch_array($Login_Query)) {
   $Latest_time = strtotime($Login['Logged_In']);
   if(($current_time - $Latest_time) < ($current_time - $most_recent)) {
      $most_recent = $Latest_time;
      $Login_ID = $Login['Login_ID'];
   }
}?>

 

Ken

Link to comment
Share on other sites

that wont work.

 

i am storing all login details in a database

first it write a new"login record, with a unique login id. but what  i want to do is to find the most recent login ( ie the one that just happened )  so thatg i can store the login id in a cookie, so that i can easily pass the database throughout the members are,

 

if theres a better way of doing this, then please tell me.

 

i originally wanted to just use cookies, but i want to store how much time a use is logged in for, so this is the best

 

the database field is a timestampe, and is default to the the surrent timestamp, ie, NOW, but when we try to get that recond out again acording to timestamp, the timestamp will be different becausae of the number of milli seconds.........

 

 

if theres a better way, or my plan is completely screwed up, please let me know

thanks for your help, PC Nerd

Link to comment
Share on other sites

<?php
   $Login = mysqli_fetch_array($Login_Query);
do{
   $Latest_time = $Login['Logged_In'];
}while($Login = mysqli_fetch_array($Login_Query)); 
?>

 

That is the correct syntax for the do,while.

 

Do whiles are for when you always want certain code to execute. But you have a flaw for an infinite loop by putting the $Login inside the do while, because it never loops if that makes any sense. I would suggest you look into the type of operations you are trying to do before just doing them. IE read up on Do Whiles and the correct syntax.

Link to comment
Share on other sites

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.