Jump to content

[SOLVED] adding more then 1 WHERE clause in a query


runnerjp

Recommended Posts

ok currently i have this

<?php $result = mysql_query("SELECT * FROM useronline WHERE(file='http://www.runningprofiles.com/members/index.php?page=mainforum')");
while($row = mysql_fetch_array( $result )) {

$last_active = time() - $row['timestamp'];
$onlineuser = $row['user'];
}

if($last_active < 300) {
echo $onlineuser;
}
?>

 

which should get all the users that are viewing the page http://www.runningprofiles.com/members/index.php?page=mainforum'

 

but i really want to get all the users on my profile which has pages suck as

 

http://www.runningprofiles.com/members/index.php?page=forum&forum=general

http://www.runningprofiles.com/members/index.php?page=forum&forum=races

 

but how can i also add these into the query

 

Link to comment
Share on other sites

Use "OR"    i.e.

 

mysql_query("SELECT * FROM useronline WHERE(file='http://www.runningprofiles.com/members/index.php?page=mainforum') OR (file='
http://www.runningprofiles.com/members/index.php?page=forum&forum=general') OR ('http://www.runningprofiles.com/members/index.php?page=forum&forum=races'")

 

that should work

 

:)

Link to comment
Share on other sites

Something along the lines of

 

SELECT  * FROM useronline WHERE file IN ('http://www.runningprofiles.com/members/index.php?page=mainforum','http://www.runningprofiles.com/members/index.php?page=general')"

 

You can add as many pages to the list as you want

Link to comment
Share on other sites

humm ok i need to go back 1 step first... i used the simple part

 

<?php $result = mysql_query("SELECT * FROM useronline WHERE(file='http://www.runningprofiles.com/members/index.php?page=forum&forum=$forum')");
while($row = mysql_fetch_array( $result )) {

$last_active = time() - $row['timestamp'];
$onlineuser = $row['user'];
}

if($last_active < 300) {
echo $onlineuser;
}
?>

 

but all it displays is 1 user... the 1st user i think on the page not them all

 

i have boith user1 and user2 on the page but its only sayin user 1 is on the page?? :S

Link to comment
Share on other sites

http://dev.mysql.com/doc/refman/4.1/en/func-op-summary-ref.html :)

 

Edit to add the rest of the post pre-emptive post click!

 

I would do something like

 

//snip query
$activeTime = 300; //max inactivity time
$onlineUsers = array();

while($row = mysql_fectch_array($result)) {
    if((time() - $row['timestamp']) < $activeTime) {
      $onlineUsers[] = $row['user'];
    }
}

if(count($onlineUsers) > 0) {
    echo join($onlineUsers, " ");
}

Link to comment
Share on other sites

humm ok i added the code

 

<?php $result = mysql_query("SELECT * FROM useronline WHERE(file='http://www.runningprofiles.com/members/index.php?page=forum&forum=$forum')");
$activeTime = 300; //max inactivity time
$onlineUsers = array();

while($row = mysql_fectch_array($result)) {
    if((time() - $row['timestamp']) < $activeTime) {
      $onlineUsers[] = $row['user'];
    }
}

if(count($onlineUsers) > 0) {
    echo join($onlineUsers, " ");
}

?>

but for somereason it gets ridd of everythin under it and does not display any users

Link to comment
Share on other sites

ok i tired this

 

<?php
include '../settings.php';
$result = mysql_query("SELECT * FROM useronline");

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

			$last_active = time() - $row['timestamp'];
			$onlineuser = $row['user'];				
			if ($last_active < 3600)
			{

    
if(count($onlineuser) > 0) {
    echo join($onlineuser, " ");
}
}
}

?>

 

b8t i get Warning: join() [function.join]: Invalid arguments passed in /home/runningp/public_html/members/test.php on line 15

 

Link to comment
Share on other sites

<?php

include '../settings.php';

if ($result = mysql_query("SELECT * FROM useronline")) {
  if (mysql_num_rows($result)) {
    while ($row = mysql_fetch_array($result)) {
      $last_active = time() - $row['timestamp'];				
      if ($last_active < 3600) {
        $onlineuser .= $row['user'] . " ";
      }
    }
  }
}

echo $onlineuser;

?>

Link to comment
Share on other sites

ahh yes i see now...

 

ok but how would i make it look like the php version

 

runnerjp, roopurt18, thorpe, jamerson, discomatt, mikenl, SnakeFox, Monk3h, mikelmao, ober, Darkmatter5, Shaba, sarab99, sinista and 105 Guests are viewing this board.

 

but instead of guests its just

 

runnerjp,roopurt18 and thorpe

Link to comment
Share on other sites

ok but how would i make it look like the php version

 

Um.. it is php.

 

To get the kind of output you want, one way would be to use an array.

 

<?php

include '../settings.php';

if ($result = mysql_query("SELECT * FROM useronline")) {
  if (mysql_num_rows($result)) {
    while ($row = mysql_fetch_array($result)) {
      $last_active = time() - $row['timestamp'];				
      if ($last_active < 3600) {
        $onlineuser[] = $row['user'];
      }
    }
  }
}

for ($i=0;$i<count($onlineusers);$i++) {
  if ($i == count($onlineusers)-1) {
    echo " and " . $onlineusers[$i];
  } else {
    echo " " . $onlineusers[$i];
  }
}

?>

Link to comment
Share on other sites

Oh man... do I really need to write the entire thing?

 

<?php

include '../settings.php';

if ($result = mysql_query("SELECT * FROM useronline")) {
  if (mysql_num_rows($result)) {
    while ($row = mysql_fetch_array($result)) {
      $last_active = time() - $row['timestamp'];				
      if ($last_active < 3600) {
        $onlineuser[] = $row['user'];
      }
    }
  }
}

if (isset($onlineusers)) {
  if (count($onlineusers) == 1) {
    echo $onlineusers[0];
  } else {
    for ($i=0;$i<count($onlineusers);$i++) {
      if ($i == count($onlineusers)-1) {
        echo " and " . $onlineusers[$i];
      } else {
        echo " " . $onlineusers[$i];
      }
    }
  }
}

?>

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.