Jump to content


This topic is now archived and is closed to further replies.


Mysql Problem 2

Recommended Posts

What do I do to my code to make it show the last one row entered?

$sql="SELECT * FROM users ORDER BY id DESC";
while ($row=mysql_fetch_array($mysql_result))
echo "<center><font color='#C0C0C0'>-Newest User-</font><br /><a href='View_Profile.php?Username=$Username'>$Username</a><br /><font color='Black'>$Title</font></center>";
?>  [/code]

Share this post

Link to post
Share on other sites
That's not correct.  If you have an auto incrementing ID then the highest one will be the most recent.  I'd use the following code to get the data...


// Query selects the last user added to the database and only returns one row
$sql = "SELECT * FROM users ORDER BY id DESC LIMIT 1";

// I've changed $mysql_result to $result as there's a function called mysql_result() wouldn't want it getting confusing
$result = mysql_query($sql) or die ("Couldn't execute:<br>\n$sql<br>\n". mysql_error());

$row = mysql_fetch_array($result, MYSQL_ASSOC));

//echo the code
echo "<center><font color='#C0C0C0'>-Newest User-</font><br /><a href='View_Profile.php?Username={$row['Username']}'>{$row['Username']}</a><br /><font color='Black'>$Title</font></center>";


Share this post

Link to post
Share on other sites


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.