Jump to content

[SOLVED] retrieving last record from mysql


MikeDXUNL

Recommended Posts

yeah i knew that, but like i said; i dont want to use two seperate queries. i am also using $result to display all the data on the page.

 

and dclamp;

your method would work, but some id's are not displayed with my query.

some items in the database are > NOW() so $last would not always equal the last id selected from the database

Link to comment
Share on other sites

then move all of them into an array first....

 

$result =  mysql_query("SELECT * FROM news WHERE date <= NOW()") or die(mysql_error());
$data = array();
while($row = mysql_fetch_array($result)){
  $data[] = $row;
}
$last = $data[count($data)-1];

Link to comment
Share on other sites

then move all of them into an array first....

 

$result =  mysql_query("SELECT * FROM news WHERE date <= NOW()") or die(mysql_error());
$data = array();
while($row = mysql_fetch_array($result)){
  $data[] = $row;
}
$last = $data[count($data)-1];

 

ah missed that part...

Link to comment
Share on other sites

Actually, I don't think dclamps code would work. It is using a count() on a mysql result resource. Should be using a mysql_num_rows(). But, you could use something like that without dumping the results into an array first:

 

$result =  mysql_query("SELECT * FROM news WHERE date <= NOW() ORDER BY id ASC") or die(mysql_error());

//Add last record to a new array
$last_record = mysql_result($result, mysql_num_rows($result)-1);

//Run through all results from query
while ($record = mysql_fetch_assoc($result))
{
    // Do something
}

Link to comment
Share on other sites

when you use mysql_fetch_array() it comes back as an array, then you can use count() to get the last row.

 

mysql_fetch_array() fetches one record as an array - not the entire result set. The OP is looking for the last record int he result set.

 

i tried giving the OP a query for the last record, but they wanted ALL the records AND the last record. that is why you have to do the loop and then use count(). besides...the OP marked this SOLVED

Link to comment
Share on other sites

No disrespect intended. I was just making a correction (you can't use count() on a result set as posted in Reply #2, was not referring to the array solution) and showing another alternative to get both all the records and the last record independantly without dumping the results into an array and without a 2nd query.

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.