RynMan Posted March 9, 2010 Share Posted March 9, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/194610-last-record-of-query/ Share on other sites More sharing options...
Zane Posted March 9, 2010 Share Posted March 9, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/194610-last-record-of-query/#findComment-1023519 Share on other sites More sharing options...
RynMan Posted March 9, 2010 Author Share Posted March 9, 2010 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). Quote Link to comment https://forums.phpfreaks.com/topic/194610-last-record-of-query/#findComment-1023525 Share on other sites More sharing options...
Zane Posted March 9, 2010 Share Posted March 9, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/194610-last-record-of-query/#findComment-1023528 Share on other sites More sharing options...
RynMan Posted March 9, 2010 Author Share Posted March 9, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/194610-last-record-of-query/#findComment-1023535 Share on other sites More sharing options...
Zane Posted March 9, 2010 Share Posted March 9, 2010 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; Quote Link to comment https://forums.phpfreaks.com/topic/194610-last-record-of-query/#findComment-1023543 Share on other sites More sharing options...
RynMan Posted March 9, 2010 Author Share Posted March 9, 2010 Ahhh! That worked perfectly! Thanks very much Zane. Question though: Why does the "$surname[]" need to have the square brackets? Quote Link to comment https://forums.phpfreaks.com/topic/194610-last-record-of-query/#findComment-1023556 Share on other sites More sharing options...
Zane Posted March 9, 2010 Share Posted March 9, 2010 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++; } Quote Link to comment https://forums.phpfreaks.com/topic/194610-last-record-of-query/#findComment-1023560 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.