Re: [PHP-DB] last record
When the result page is presented I want to provide a link to the last record at the top of the page. well, what about one of these: - add that link via javascript - use some sort of a buffer valiable for the output and add the link at the beginning of the buffer from within php -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DB] last record
neil wrote: Thanks Peter The reason I was wanting to do it in php was because the sql query is quite complex and variable depending on the input from a form. When the result page is presented I want to provide a link to the last record at the top of the page. After connecting and selecting the table I am constructing the query I then want pick off the last record and then get all the records the rather cludgy way I am doing it is this: First run $result = mysql_query($sqlstr. desc limit 0,1); $row = mysql_fetch_assoc($result); Second run $result = mysql_query($sqlstr); while ($row = mysql_fetch_assoc($result)) { This is fine while there is only one order by appended to the query but if there is none or more than one it doesn't work so well ^ it doesn't work so well because you're only desc ordering the last item, instead of all of them. You'll want to track all the items you're trying to order by, and build a separate ending for the two queries from them. This will work well if mysql can and actually does cache the result - but if it doesn't, then it will have to analyze and run both queries in full. Is there any reason you can't pull all the data in from the query - tossing it in an array, then emit the last row, then move on to looping over the array? Those are pretty much your options, as I see it - either burn memory in php storing the result set, or do the query twice. Are you memory limited or cpu limited? :) There might be a way to move back and forth along a mysql result set - maybe you can push all the way to the end, then reset its 'pointer' and get it from the beginning again... [from the manual] mysql_data_seek() moves the internal row pointer of the MySQL result associated with the specified result identifier to point to the specified row number. The next call to mysql_fetch_row() would return that row. Row_number starts at 0. The row_number should be a value in the range from 0 to mysql_num_rows - 1. [/snip] So - move to mysql_num_rows() - 1, get the row, then move to 0 and do your while. Neat - learned something new. Cheers, -- - Martin Norland, Database / Web Developer, International Outreach x3257 The opinion(s) contained within this email do not necessarily represent those of St. Jude Children's Research Hospital. -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP-DB] last record
Apart from the obvious of reversing the sort order in a select statement and then picking off the first record eg. select * from blah where somefield like '%something%' order by someotherfield desc limit 0,1 is there any way in php rather than sql to pick the last record in a record set? Thanks Neil -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DB] last record
neil wrote: Apart from the obvious of reversing the sort order in a select statement and then picking off the first record eg. select * from blah where somefield like '%something%' order by someotherfield desc limit 0,1 is there any way in php rather than sql to pick the last record in a record set? Unless you're trying to recreate mysql_insert_id(), then the way you have written is the way to do it. I suppose you could use mysql_result(), but that'd be pretty inefficient. -- ---John Holmes... Amazon Wishlist: www.amazon.com/o/registry/3BEXC84AB3A5E/ php|architect: The Magazine for PHP Professionals www.phparch.com -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DB] last record
Thanks Peter The reason I was wanting to do it in php was because the sql query is quite complex and variable depending on the input from a form. When the result page is presented I want to provide a link to the last record at the top of the page. After connecting and selecting the table I am constructing the query I then want pick off the last record and then get all the records the rather cludgy way I am doing it is this: First run $result = mysql_query($sqlstr. desc limit 0,1); $row = mysql_fetch_assoc($result); Second run $result = mysql_query($sqlstr); while ($row = mysql_fetch_assoc($result)) { This is fine while there is only one order by appended to the query but if there is none or more than one it doesn't work so well Thanks Neil -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP-DB] last record
hi using mysql as you describe is probably the most elegant and efficient way to do it is there a reason for asking php to do it? Peter -Original Message- From: neil [mailto:[EMAIL PROTECTED] Sent: 25 January 2005 03:12 To: php-db@lists.php.net Subject: [PHP-DB] last record Apart from the obvious of reversing the sort order in a select statement and then picking off the first record eg. select * from blah where somefield like '%something%' order by someotherfield desc limit 0,1 is there any way in php rather than sql to pick the last record in a record set? Thanks Neil -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP-DB] Last Record
Is there a quick and easy way to find out the record id of the last record in a table? I need to read the last record and let the user know what the date of the last record added is. -- Jim Freedom is worth protecting -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP-DB] Last Record
Is there a quick and easy way to find out the record id of the last record in a table? I need to read the last record and let the user know what the date of the last record added is. SELECT date FROM table ORDER BY date DESC LIMIT 1 ---John W. Holmes... PHP Architect - A monthly magazine for PHP Professionals. Get your copy today. http://www.phparch.com/ -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP-DB] Last record
If I have just inserted one set of values with mysql_query(INSERT ...) how can I get the values just inserted (I needed to get an autoincrement value out of the table) Thanks Antonio Bernabei - Original Message - From: César L. Aracena [EMAIL PROTECTED] To: PHP DB List [EMAIL PROTECTED] Sent: Monday, June 03, 2002 9:05 AM Subject: [PHP-DB] Strange need Hi all, I know this is not used a lot, but I'm building a site that will show up a some kinda strange content. It will have diferent tables inside diferent pages. Each table will show a diferent header with several colums showing diferent diameters of the products. The table header will not be created by PHP, so the colums diameters will not be handled or recordered using variables. The rows on the other hand, will show diferent products which will be obtained by PHP from a MySQL database. each product has several diameters and I need PHP to put a check sign (image made) under ceratin diameter colums for each product. The tables under I have that information are a products table, a diameters table and a relationship of 1:N table for the products - diameters. Can I make a PHP function that will recognize the HTML column content and compare it to the information collected from the DB relationships table, so it will know when to put a check sign in the column for that product? Thanks a lot on this one, Cesar Aracena [EMAIL PROTECTED] Neuquen, Argentina -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DB] Last record
You can solve that just by asking for the insert_id that was made. Just place the following lines after the mysql_query: $id = mysql_insert_id(); $message = Record Added (id = $id); and you'r done! Cesar Aracena [EMAIL PROTECTED] Neuquen, Argentina - Original Message - From: Antonio Bernabei [EMAIL PROTECTED] To: César L. Aracena [EMAIL PROTECTED]; PHP DB List [EMAIL PROTECTED] Sent: Monday, June 03, 2002 5:14 AM Subject: [PHP-DB] Last record If I have just inserted one set of values with mysql_query(INSERT ...) how can I get the values just inserted (I needed to get an autoincrement value out of the table) Thanks Antonio Bernabei - Original Message - From: César L. Aracena [EMAIL PROTECTED] To: PHP DB List [EMAIL PROTECTED] Sent: Monday, June 03, 2002 9:05 AM Subject: [PHP-DB] Strange need Hi all, I know this is not used a lot, but I'm building a site that will show up a some kinda strange content. It will have diferent tables inside diferent pages. Each table will show a diferent header with several colums showing diferent diameters of the products. The table header will not be created by PHP, so the colums diameters will not be handled or recordered using variables. The rows on the other hand, will show diferent products which will be obtained by PHP from a MySQL database. each product has several diameters and I need PHP to put a check sign (image made) under ceratin diameter colums for each product. The tables under I have that information are a products table, a diameters table and a relationship of 1:N table for the products - diameters. Can I make a PHP function that will recognize the HTML column content and compare it to the information collected from the DB relationships table, so it will know when to put a check sign in the column for that product? Thanks a lot on this one, Cesar Aracena [EMAIL PROTECTED] Neuquen, Argentina -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php