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. Quote Link to comment 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); Quote Link to comment 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"; Quote Link to comment 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. Quote Link to comment 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) Quote Link to comment 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> Quote Link to comment Share on other sites More sharing options...
monkeytooth Posted March 17, 2009 Share Posted March 17, 2009 try echo $result['username']; Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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? Quote Link to comment 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 Quote Link to comment 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?!?! ??? Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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 Quote Link to comment 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.