Jump to content

Recommended Posts

I am a fairly new to PHP, and I want to write a IF statement.

 

How can I pull information from a MySQL database, and write a if statement with that information.

 

for example, I pull the username Lamez, from the database, and if Lamez is online, the statement will output James is Online, but if Lamez was offline the statement would output Lamez is offline.

 

-Thanks Guys

Well, assuming you have some sort of "last active" field in the database, you could do this.

 

<?php

$query = mysql_query("SELECT username FROM table WHERE username='Lamez' and last_active < (NOW() - INTERVAL 5 MIN)");

if (mysql_num_rows($query) > 0){
   echo "Online";
} else {
   echo "Offline";
}

?>

I do have, it is called active_users

 

it has the tables username, and timestamp

 

I also have other tables called:

 

active_guests

banned_users

users

 

now I am getting these errors

 

Warning: mysql_query() [function.mysql-query]: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) in /mounted-storage/home48c/sub007/sc33591-LWQU/www/blue/active.php on line 3

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /mounted-storage/home48c/sub007/sc33591-LWQU/www/blue/active.php on line 3

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /mounted-storage/home48c/sub007/sc33591-LWQU/www/blue/active.php on line 5

 

here is my edited code:

 

<?php

$query = mysql_query("SELECT username FROM table WHERE username='test1' and active_users < (NOW() - INTERVAL 5 MIN)");

if (mysql_num_rows($query) > 0){
   echo "Test1 is Online";
} else {
   echo "Test1 is Offline";
}

?>

 

what am I doing wrong?

 

You need to connect to the mysql host and select a database.

 

<?php
$conn = @mysql_connect("localhost", "root", "neveruseasroot");

if( !$conn ) 
    die("Error connecting to MySQL host");

$db = @mysql_select_db("database",$conn);

if( !$db )
    die("Error selecting a database");

//... continue with your code
?>

aw, I feel retarded, but now I get this error.

 

 

 

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /mounted-storage/home48c/sub007/sc33591-LWQU/www/blue/active.php on line 17

I have no idea what line 17 is saying.

 

here is line 17

if (mysql_num_rows($query) > 0){

 

 

here is the full code

<?php
include "style/include/constants.php";
include "style/include/database.php";


/*if( !$conn ) 
    die("Error connecting to MySQL host");

$db = @mysql_select_db("database",$conn);

if( !$db )
    die("Error selecting a database");*/


$query = mysql_query("SELECT username FROM table WHERE username='test1' and active_users < (NOW() - INTERVAL 5 MIN)");

if (mysql_num_rows($query) > 0){
   echo "Test1 is Online";
} else {
   echo "Test1 is Offline";
}

?>

 

Thanks Guys, you are great

It's because your query isn't right. You said that "active_users" is the name of another TABLE, right? If it is, then you can't use it in your query like it's a row in whatever table the username is in.

 

What is the name of the table that stores the username, and what is the table structure of the "active_users" table?

I am tellin ya you are dealing with a real noob here,

 

now I am getting this error:

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''MIN')' at line 1

 

I have never seen a error like that in my life.

Oops, I don't know why I put "MIN" instead of "MINUTE", haha.

 

Change it to

$query = mysql_query("SELECT u.username FROM users u LEFT JOIN active_users a ON a.username=u.username WHERE a.timestamp < (NOW() - INTERVAL 5 MINUTE)")or die(mysql_error());

I was thinking you might be back with that question ;) Lets try an INNER JOIN instead.

 

$query = mysql_query("SELECT u.username FROM users u INNER JOIN active_users a ON a.username=u.username WHERE a.timestamp < (NOW() - INTERVAL 5 MINUTE)")or die(mysql_error());

no not this time  :(.

 

you are not including a username at all, you are just saying any username.

I am no PHP or MySQL expert here, but shouldn't ya include some the username we want online or offline?

 

-thanks a bunch, I really do thank you for your time and help.

DUH! I am having all sorts of brain farts today >.>

 

$query = mysql_query("SELECT u.username FROM users u INNER JOIN active_users a ON a.username=u.username WHERE a.timestamp < (NOW() - INTERVAL 5 MINUTE) AND u.username='insert_username_here'")or die(mysql_error());

 

Put the username where it says "insert_username_here".

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.