Jump to content

PHP/MySQL question


denhamd2

Recommended Posts

Hi,

 

I have a table users in my MySQL database

 

I'm looking to select the most recently entered user record (they are indexed by the userid auto incrementing field)

 

then I would like to echo this userid

 

any ideas on the PHP/MySQL code for this?

 

Many thanks in advance

Link to comment
Share on other sites

well if its auto increment,

 

you could either go through two queries, one to find the number of records on the database, and chose the highest number at the recods id which is how id do it, or you could get al the records, and loop throught ehem using while, then that last one would have been the one that you want

 

good luck

Link to comment
Share on other sites

this is the code I'm trying to use, but it just prints out all userid's, i just want it to print out the most recent one. Any ideas on how to change this code to make it work?

 

$result = mysql_query("SELECT * FROM users ORDER BY userid ASC");
while($row = mysql_fetch_array($result))
  {
  echo $row['userid'];
  }

Link to comment
Share on other sites

You could just count how many there are and then echo that one, if thats what you mean.

 

Like:

 

<?php
$query = "SELECT * FROM users";
$result = mysql_query($query) or die (mysql_error());
$total = @mysql_num_rows($result);

// error check

if ($total == 0)
  {
echo "None to show <br />";
}else{

// get the last one

$id = $total;

$query = "SELECT * FROM users WHERE id = '$id'";
$result = mysql_query($query) or die (mysql_error());
$row = @mysql_fetch_array($result);

// error check

if (!$row)
  {
echo "None to show";
}else{
// set your values here
echo $total;
  }
}

?>

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.