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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.