Jump to content

Mysql Problem 2


ShanesProjects

Recommended Posts

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

[code]<?
include("MySQL_Info.php");
$sql="SELECT * FROM users ORDER BY id DESC";
$mysql_result=mysql_query($sql);
$num_rows=mysql_num_rows($mysql_result);
while ($row=mysql_fetch_array($mysql_result))
{
$Username=$row["Username"];
$Title=$row["Title"];
}
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]
Link to comment
https://forums.phpfreaks.com/topic/24465-mysql-problem-2/
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...

[code]
<?php
include("MySQL_Info.php");

// 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>";
?>
[/code]

Regards
Huggie
Link to comment
https://forums.phpfreaks.com/topic/24465-mysql-problem-2/#findComment-111486
Share on other sites

Archived

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

×
×
  • Create New...

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.