Jump to content

Recommended Posts

Hi guys

 

Bit of a PHP Rookie here so go easy.  :(

 

I'm basically wanting to display the last row of a query. 

 

SELECT * FROM tblclientinformation  LIMIT 24

 

That's my query and I can call this using PHP and everything.  But I just want the Last name of the client who is the 24th row in the query.  How on earth can I get this information?  I'm quessing the query is stored in some sort of array when I use the mysql_fetch_array function on the variable containing my query function......but I just have no idea where to go next.

 

Thanks for any help guys. 

Link to comment
https://forums.phpfreaks.com/topic/194610-last-record-of-query/
Share on other sites

If you only want the low row then you should use this query..  Assuming you have an id column

Select * from tblclientinformation ORDER BY id DESC LIMIT 1

 

If you only want the last name in the last row then you should use something like this

Select last_name from tblclientinformation ORDER BY id DESC LIMIT 1

If you only want the low row then you should use this query..  Assuming you have an id column

Select * from tblclientinformation ORDER BY id DESC LIMIT 1

 

If you only want the last name in the last row then you should use something like this

Select last_name from tblclientinformation ORDER BY id DESC LIMIT 1

 

Thanks Zane.

 

I actually need the information from the query.  I'm basically using 'while' to display the 24 last names in the query.  I need to know the last name in the query before the page loads information so I can parse it to the next page etc. 

 

The query you gave me would basically just give me the last record in the table, would it not?  I actually need the last record of the top 24 records pulled in the query (the table itself has severla hundred records).

I see.  My guess is that you're doing pagination, correct?  Pulling 24 records at a time for each page, etc, etc.

 

If you check out the documentation, the SQL documentation you'll find out that doing such a thing is as simple as adding a comma to your LIMIT part of the query.  Let me explain

 

This will pull the first 24 records

SELECT * FROM tblclientinformation  LIMIT 24

 

This will pull records 25 through 49

SELECT * FROM tblclientinformation  LIMIT 25, 49

This will pull records 50 through 97

SELECT * FROM tblclientinformation  LIMIT 50, 97

 

and so on and so on

I see.  My guess is that you're doing pagination, correct?  Pulling 24 records at a time for each page, etc, etc.

 

If you check out the documentation, the SQL documentation you'll find out that doing such a thing is as simple as adding a comma to your LIMIT part of the query.  Let me explain

 

This will pull the first 24 records

SELECT * FROM tblclientinformation  LIMIT 24

 

This will pull records 25 through 49

SELECT * FROM tblclientinformation  LIMIT 25, 49

This will pull records 50 through 97

SELECT * FROM tblclientinformation  LIMIT 50, 97

 

and so on and so on

 

Wow, I did not know you could do that!  That certainly makes it easier.  :)  Thanks Zane.

 

However, I'll still need to know the name of that 24th person, as I want to echo the text to display the 'to' and 'from' names (i.e.  Adams to Jones).  Basically display the range of names on the page. 

That is where you would use PHP.. ... hmm.. lets see

 

 

assuming you know the basics of PHP/mysql fetching..

Since you'd be using a loop to do all this it'd be quite easy to do..  Just make an array of all these names.. and only use the first and last ones... on each page

 

so in your while loop.. the mysql_fetch_array one.. it would be something like

while($row = mysql_fetch_array($result)) {
   $surname[] = $row['last_name'];
}
$toFromHeader = "From {$surname[0]} to {$surname[23]}";
echo $toFromHeader;

by using the square brackets you are distinctly telling PHP that $surname is going to be an array without having to set it beforehand.  It's much easier that way.  Otherwise you'd have to do it this way.

 

$i = 0;

$surname = array(); // Even if you didn't put this it would still work.

while($row = mysql_fetch_array($result)) {

    $surname[$i] = $row['last_name'];

    $i++;

}

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.