Lamez Posted December 9, 2007 Share Posted December 9, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/80911-solved-mysql-pulling-data-php-if-statment/ Share on other sites More sharing options...
pocobueno1388 Posted December 9, 2007 Share Posted December 9, 2007 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"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/80911-solved-mysql-pulling-data-php-if-statment/#findComment-410468 Share on other sites More sharing options...
Lamez Posted December 9, 2007 Author Share Posted December 9, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/80911-solved-mysql-pulling-data-php-if-statment/#findComment-410491 Share on other sites More sharing options...
rab Posted December 9, 2007 Share Posted December 9, 2007 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 ?> Quote Link to comment https://forums.phpfreaks.com/topic/80911-solved-mysql-pulling-data-php-if-statment/#findComment-410496 Share on other sites More sharing options...
pocobueno1388 Posted December 9, 2007 Share Posted December 9, 2007 Your not connected to the database. Also add a or die(mysql_error()) to the end of your query. Quote Link to comment https://forums.phpfreaks.com/topic/80911-solved-mysql-pulling-data-php-if-statment/#findComment-410497 Share on other sites More sharing options...
Lamez Posted December 9, 2007 Author Share Posted December 9, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/80911-solved-mysql-pulling-data-php-if-statment/#findComment-410512 Share on other sites More sharing options...
pocobueno1388 Posted December 9, 2007 Share Posted December 9, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/80911-solved-mysql-pulling-data-php-if-statment/#findComment-410514 Share on other sites More sharing options...
Lamez Posted December 9, 2007 Author Share Posted December 9, 2007 users has all the username, password, level, id, timestamp and email. I am not sure what you are asking on structure, but it has the rows: username, and timestamp Quote Link to comment https://forums.phpfreaks.com/topic/80911-solved-mysql-pulling-data-php-if-statment/#findComment-410516 Share on other sites More sharing options...
pocobueno1388 Posted December 9, 2007 Share Posted December 9, 2007 Okay, try this query. Make sure all the field and table names are correct SELECT u.username FROM users u LEFT JOIN active_users a ON a.username=u.username WHERE a.timestamp < (NOW() - INTERVAL 5 MIN) Quote Link to comment https://forums.phpfreaks.com/topic/80911-solved-mysql-pulling-data-php-if-statment/#findComment-410517 Share on other sites More sharing options...
Lamez Posted December 9, 2007 Author Share Posted December 9, 2007 its looks good, so would I 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 MIN)"); Quote Link to comment https://forums.phpfreaks.com/topic/80911-solved-mysql-pulling-data-php-if-statment/#findComment-410520 Share on other sites More sharing options...
pocobueno1388 Posted December 9, 2007 Share Posted December 9, 2007 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 MIN)")or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/80911-solved-mysql-pulling-data-php-if-statment/#findComment-410522 Share on other sites More sharing options...
Lamez Posted December 9, 2007 Author Share Posted December 9, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/80911-solved-mysql-pulling-data-php-if-statment/#findComment-410526 Share on other sites More sharing options...
pocobueno1388 Posted December 9, 2007 Share Posted December 9, 2007 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()); Quote Link to comment https://forums.phpfreaks.com/topic/80911-solved-mysql-pulling-data-php-if-statment/#findComment-410560 Share on other sites More sharing options...
Lamez Posted December 9, 2007 Author Share Posted December 9, 2007 omg, thank you so much! If I had a virtual dollar, I would give you it. Thanks once again. Quote Link to comment https://forums.phpfreaks.com/topic/80911-solved-mysql-pulling-data-php-if-statment/#findComment-410574 Share on other sites More sharing options...
Lamez Posted December 10, 2007 Author Share Posted December 10, 2007 ok I lied, it says online to anyone who logs on. how can I make it just one user? Quote Link to comment https://forums.phpfreaks.com/topic/80911-solved-mysql-pulling-data-php-if-statment/#findComment-410588 Share on other sites More sharing options...
pocobueno1388 Posted December 10, 2007 Share Posted December 10, 2007 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()); Quote Link to comment https://forums.phpfreaks.com/topic/80911-solved-mysql-pulling-data-php-if-statment/#findComment-410595 Share on other sites More sharing options...
Lamez Posted December 10, 2007 Author Share Posted December 10, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/80911-solved-mysql-pulling-data-php-if-statment/#findComment-410602 Share on other sites More sharing options...
pocobueno1388 Posted December 10, 2007 Share Posted December 10, 2007 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". Quote Link to comment https://forums.phpfreaks.com/topic/80911-solved-mysql-pulling-data-php-if-statment/#findComment-410603 Share on other sites More sharing options...
Lamez Posted December 10, 2007 Author Share Posted December 10, 2007 Thanks a billion, that did work. I was reading the code, and was wondering why there was no certain username. thanks again Quote Link to comment https://forums.phpfreaks.com/topic/80911-solved-mysql-pulling-data-php-if-statment/#findComment-410607 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.