supergrame Posted March 17, 2009 Share Posted March 17, 2009 i have a table users and a field username and a field time all i want to do is get the last person entered into the database via the time and put it in a variable so i can echo it out. i have been looking through some sample code but it just doesn't seem to work. Link to comment https://forums.phpfreaks.com/topic/149827-solved-get-user-name-with-latest-timestamp/ Share on other sites More sharing options...
monkeytooth Posted March 17, 2009 Share Posted March 17, 2009 if you can get the information from the database and pull it out as a variable you can then echo it out from there.. if your looking to convert a time stamp, if your using a unix timestamp just do something like strtotime("g:ia", $varfortimestamp); Link to comment https://forums.phpfreaks.com/topic/149827-solved-get-user-name-with-latest-timestamp/#findComment-786755 Share on other sites More sharing options...
aebstract Posted March 17, 2009 Share Posted March 17, 2009 $query = "SELECT username FROM users ORDER BY time LIMIT 1"; Link to comment https://forums.phpfreaks.com/topic/149827-solved-get-user-name-with-latest-timestamp/#findComment-786757 Share on other sites More sharing options...
jlhaslip Posted March 17, 2009 Share Posted March 17, 2009 SELECT $user, $time FROM users ORDER BY $time DESC LIMIT 1; *edit* Phone call delayed my reply, sorry. Link to comment https://forums.phpfreaks.com/topic/149827-solved-get-user-name-with-latest-timestamp/#findComment-786764 Share on other sites More sharing options...
supergrame Posted March 17, 2009 Author Share Posted March 17, 2009 this is what i used $sql = "SELECT username FROM users ORDER BY time LIMIT 1"; $result=mysql_query($sql) or die(mysql_error()); echo $result; this is what the output was not the username. Resource id #10 is that becuase of this mysql_query($sql) Link to comment https://forums.phpfreaks.com/topic/149827-solved-get-user-name-with-latest-timestamp/#findComment-786780 Share on other sites More sharing options...
jlhaslip Posted March 17, 2009 Share Posted March 17, 2009 You need to fetch the array of results. $result = @mysql_query ($query); // Run the query. if (mysql_num_rows($result) == 1) { // Valid user ID, show the form. // Get the user's information. $row = mysql_fetch_array ($result, MYSQL_NUM); // Create the form. echo '<h2>Delete a User</h2> <form action="delete_user.php" method="post"> <h3>Name: ' . $row[0] . '</h3> Link to comment https://forums.phpfreaks.com/topic/149827-solved-get-user-name-with-latest-timestamp/#findComment-786784 Share on other sites More sharing options...
monkeytooth Posted March 17, 2009 Share Posted March 17, 2009 try echo $result['username']; Link to comment https://forums.phpfreaks.com/topic/149827-solved-get-user-name-with-latest-timestamp/#findComment-786785 Share on other sites More sharing options...
supergrame Posted March 17, 2009 Author Share Posted March 17, 2009 that made little scene to me, sorry i am just a newbie, maybe id there is an easy way for me to understand. all i want to do is is fetch the latest date/time from time field and relate that to the username and then echo out that user name. Link to comment https://forums.phpfreaks.com/topic/149827-solved-get-user-name-with-latest-timestamp/#findComment-786801 Share on other sites More sharing options...
aebstract Posted March 17, 2009 Share Posted March 17, 2009 Use what I said, and do what monkeytooth said: try echo $result['username']; You can also change 'username' in the code I posted to '*' to return all columns of the row you get. Link to comment https://forums.phpfreaks.com/topic/149827-solved-get-user-name-with-latest-timestamp/#findComment-786806 Share on other sites More sharing options...
supergrame Posted March 17, 2009 Author Share Posted March 17, 2009 this is what i use <?php $query = "SELECT username FROM users ORDER BY time LIMIT 1"; echo $result['username']; ?> this is my error Notice: Undefined variable: result in C:\wamp\www\public\includes\news.php on line 4 ['username'] relate to the field username? Link to comment https://forums.phpfreaks.com/topic/149827-solved-get-user-name-with-latest-timestamp/#findComment-786815 Share on other sites More sharing options...
supergrame Posted March 17, 2009 Author Share Posted March 17, 2009 forget that last post the varible names dont match duh, geting tired, thanks guys Link to comment https://forums.phpfreaks.com/topic/149827-solved-get-user-name-with-latest-timestamp/#findComment-786817 Share on other sites More sharing options...
supergrame Posted March 17, 2009 Author Share Posted March 17, 2009 ok sorry not solved yet, i have this <?php $query = "SELECT username FROM users ORDER BY time LIMIT 1"; echo $query['username']; ?> output is S i have no idea why that ant even in the database?!?! ??? Link to comment https://forums.phpfreaks.com/topic/149827-solved-get-user-name-with-latest-timestamp/#findComment-786822 Share on other sites More sharing options...
aebstract Posted March 17, 2009 Share Posted March 17, 2009 $query= mysql_query("SELECT * FROM users ORDER BY time LIMIT 1") or DIE(mysql_error()); while($r=mysql_fetch_array($query)) { $username = $r["username"]; } echo "$username"; Tell me what happens when you use that. Link to comment https://forums.phpfreaks.com/topic/149827-solved-get-user-name-with-latest-timestamp/#findComment-786840 Share on other sites More sharing options...
supergrame Posted March 17, 2009 Author Share Posted March 17, 2009 that works fine no errors all good, but it only pulls the first entry everytime but i guess in that code it does not specify dec or acs! thank you for that code at least were getting somewere. Link to comment https://forums.phpfreaks.com/topic/149827-solved-get-user-name-with-latest-timestamp/#findComment-787198 Share on other sites More sharing options...
supergrame Posted March 17, 2009 Author Share Posted March 17, 2009 never mind i have got it SELECT * FROM users ORDER BY time desc LIMIT 1 thank you guys for your help Link to comment https://forums.phpfreaks.com/topic/149827-solved-get-user-name-with-latest-timestamp/#findComment-787201 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.