Jump to content


Photo

SHow current user


  • Please log in to reply
6 replies to this topic

#1 CJLeah

CJLeah
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 18 April 2006 - 07:48 PM

I'm having trouble wit a code, i'm quite sure is wrong, what I want is a code that will show the current user, eg Hello Chris. ANyway whats wrong with mine, or whats right, lol. please help :)

<?php mysql_query("SELECT username FROM users"); echo ($_POST['username']); ?>

#2 Caesar

Caesar
  • Members
  • PipPipPip
  • Advanced Member
  • 1,025 posts

Posted 18 April 2006 - 08:02 PM

First, this depends on how you determine when a user is online/offline. Do you have a cron job which deletes open sessions if they exceed a certain amount of time of inactivity? Otherwise, anytime someone logs in, and closes their browser without logging out, the session will stay open (If indeed you are using a sessions table in the database) thus, showing these people as "Online".

In regards to your example, nothing in your query gives specifics as to what user you are trying to retrieve from the database.

<?php

$username = $_POST['username'];

$somequery = mysql_query("SELECT * FROM users WHERE username = '$username'");

$result = mysql_fetch_array($somequery);

echo"Hiya $result[username], how the f*ck are you, you ugly bastage?";

?>

As far as a "who is online" script...that is trickier without knowing what I discussed above....but yeah....

Asuming that the username column is the second one in the table...

<?php

$somequery = mysql_query("SELECT * FROM users WHERE online = '1'");

while ($row = mysql_fetch_array($somequery))

{echo" $row[1] ";}

?>

I also take it you are running your login script through several checks before you determine whether someone is logged in or not...and other security concerns?

Anyway, good luck bro.
PHP Ninja

#3 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 18 April 2006 - 08:05 PM

Ok, you've shown us code that doesn't work, so now we know what you don't want.

Perhaps if you told us exactly what you are trying to do we can help you more.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#4 CJLeah

CJLeah
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 18 April 2006 - 08:16 PM

sorry, i'm veyr new to php, but basically what i'm trying to do is, show the that users username, when he look at that page, like 'Hello USERNAME how are you?' somthing like that...

that help any better?

#5 Caesar

Caesar
  • Members
  • PipPipPip
  • Advanced Member
  • 1,025 posts

Posted 18 April 2006 - 08:24 PM

[!--quoteo(post=366164:date=Apr 18 2006, 03:16 PM:name=Chris Leah)--][div class=\'quotetop\']QUOTE(Chris Leah @ Apr 18 2006, 03:16 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
sorry, i'm veyr new to php, but basically what i'm trying to do is, show the that users username, when he look at that page, like 'Hello USERNAME how are you?' somthing like that...

that help any better?
[/quote]

Look at my above example.

And consider looking through -> [a href=\"http://www.php.net\" target=\"_blank\"]http://www.php.net[/a] ....Great source.
PHP Ninja

#6 CJLeah

CJLeah
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 18 April 2006 - 08:30 PM

[!--quoteo(post=366172:date=Apr 18 2006, 03:24 PM:name=Caesar)--][div class=\'quotetop\']QUOTE(Caesar @ Apr 18 2006, 03:24 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Look at my above example.

And consider looking through -> [a href=\"http://www.php.net\" target=\"_blank\"]http://www.php.net[/a] ....Great source.
[/quote]


I have mate, but not working ..:( but i've jsut tried this also


<?php 
$result = mysql_query("SELECT COUNT(*) FROM users WHERE username = '" . $_POST['username'] . "'") or exit(mysql_error()); 
if (mysql_result($result, 0)) {echo $_POST['username'] . ' how are you?';} 
else {echo $_POST['username'] . ' is not in the database';} 
?>


#7 Caesar

Caesar
  • Members
  • PipPipPip
  • Advanced Member
  • 1,025 posts

Posted 18 April 2006 - 08:36 PM

Well..no examples will be relevent if your databae tables are named diferently, or if your code is not working properly....the example I provided...works.
PHP Ninja




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users