Jump to content


Photo

Mysql Problem 2


  • Please log in to reply
2 replies to this topic

#1 ShanesProjects

ShanesProjects
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 19 October 2006 - 06:40 PM

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

<?
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>";
?> 


#2 ShanesProjects

ShanesProjects
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 19 October 2006 - 06:50 PM

Fixed it had to be ORDER BY id ACS. Not ORDER BY id DESC.

#3 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 19 October 2006 - 07:23 PM

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

<?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>";
?>

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users