MySQL Forums
Forum List  »  PHP

Query help/question.
Posted by: d_blakejr
Date: February 22, 2006 10:00PM

I am querying the db for a list of people, and within that query I am linking to the sessions table with the persons id number to check to see if there online. The problem is if a person has two sessions, then it returns and empty row. The gc removes after 15mins I think, or on logout. But if they dont logout and then say comeback in 5mins with new browser, they get new session and there old one is still in there. Below is my query.

$query = "SELECT profile_general.id, sessions.id as session FROM profile_general LEFT JOIN sessions ON sessions.id = profile_general.id WHERE profile_image != '' ORDER BY rand() LIMIT 6";

If there is two rows in the session table with that id number, it returns an empty row, im sure if they had 3 sessions it would return 2 empty rows. Also I know that this won't happen too often but I hate the fact that it does.

I have a temp fix with php until I can figure out a solution to this. Here is what I do, but I want to be able to remove this and have the query work right.

$counter = 4;
while($row = $db->fetchByAssoc($result)) {
if($counter > 0) {
//get user information
$view_id = $row['id'];
$view = get_profile_info($view_id);

//added just in case user has two sessions
if(!in_array($view_id, $session_id)) {
$session_id[] = $view_id;

//get online
$online = ($row['session'] == $view_id) ? "<span class=\"online\">Online</span>" : "Offline";

$counter--;
}
}


Im have a LIMIT of 6 but I am only displaying 4. That way a blank row shouldnt come up ever.

So my main question is how can I optimize/fix my query to prevent empty sets?

Thanks,
Dennis

Options: ReplyQuote


Subject
Written By
Posted
Query help/question.
February 22, 2006 10:00PM
February 23, 2006 02:32AM
February 23, 2006 02:17PM
February 27, 2006 09:57PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.