oceans Posted May 2, 2007 Share Posted May 2, 2007 Dear Friends, I need help on MySQL, please advise me how I can move to last record: Situation: Say a table contains columns: (1) Number (2) Data (3) Value I have three entries the table (1) Number1 (2) Data1 (3) 1.0 (1) Number2 (2) Data1 (3) 2.0 (1) Number3 (2) Data1 (3) 3.0 I do a normal search: <?PHP $result = mysql_query("SELECT `Number` FROM `table` WHERE `Data` = 'Data1'"); } while($row = mysql_fetch_array($result)) { $InputFromDataBase[1]=$row['Number']; } ?> The output will be “Number1”, please guide me on how to go to the last record, that is I want the value to be “Number3”, Thanks. Link to comment https://forums.phpfreaks.com/topic/49585-solved-moving-to-last-record-mysql/ Share on other sites More sharing options...
trq Posted May 2, 2007 Share Posted May 2, 2007 $result = mysql_query("SELECT Number FROM `table` WHERE Data = 'Data1' ORDER BY Number DESC"); Link to comment https://forums.phpfreaks.com/topic/49585-solved-moving-to-last-record-mysql/#findComment-243103 Share on other sites More sharing options...
oceans Posted May 2, 2007 Author Share Posted May 2, 2007 Why didn't I think of that, thanks, it will work! Link to comment https://forums.phpfreaks.com/topic/49585-solved-moving-to-last-record-mysql/#findComment-243104 Share on other sites More sharing options...
oceans Posted May 2, 2007 Author Share Posted May 2, 2007 Ok, for argument sake, what if I want the middle entry? Link to comment https://forums.phpfreaks.com/topic/49585-solved-moving-to-last-record-mysql/#findComment-243108 Share on other sites More sharing options...
john010117 Posted May 2, 2007 Share Posted May 2, 2007 I don't believe there is such query for that. Link to comment https://forums.phpfreaks.com/topic/49585-solved-moving-to-last-record-mysql/#findComment-243116 Share on other sites More sharing options...
oceans Posted May 2, 2007 Author Share Posted May 2, 2007 ya what am I asking? OK, can you please teach me, how can I use "foreach statement", i remember using in "ASP ODBC", as I will not know how many record will be in the result table. That is list all onto the screen, my original one will give 1 entry, Thrope's one will give one entry, but how to show all three? Link to comment https://forums.phpfreaks.com/topic/49585-solved-moving-to-last-record-mysql/#findComment-243121 Share on other sites More sharing options...
trq Posted May 2, 2007 Share Posted May 2, 2007 <?php if ($result = mysql_query("SELECT Number FROM `table` WHERE Data = 'Data1' ORDER BY Number DESC")) { if (mysql_num_rows($result)) { while($row = mysql_fetch_assoc($result)) { echo $row['Number']; } } } ?> Link to comment https://forums.phpfreaks.com/topic/49585-solved-moving-to-last-record-mysql/#findComment-243123 Share on other sites More sharing options...
oceans Posted May 2, 2007 Author Share Posted May 2, 2007 Thanks Thrope, Can I check "mysql_fetch_assoc($result)" returns the number of records extracted "mysql_fetch_array($result)" returns a record is present the reason is I don't see a ++cnt here Link to comment https://forums.phpfreaks.com/topic/49585-solved-moving-to-last-record-mysql/#findComment-243143 Share on other sites More sharing options...
trq Posted May 2, 2007 Share Posted May 2, 2007 Each time you call mysql_fetch_* the current row is returned, and the internal pointer is moved to the next record. When the last record is fetched mysql_fetch_* returns false. This works well with a while loop because while will keep looping until the contained expression is false. The same result can be done with a for loop, but its more work.... <?php if ($result = mysql_query("SELECT Number FROM `table` WHERE Data = 'Data1' ORDER BY Number DESC")) { if ($total = mysql_num_rows($result)) { for ($i = 0;$i <= $total, $i++) { $row = mysql_fetch_assoc($result); echo $row['Number']; } } } ?> A foreach on the other hand is used to loop through arrays, and sometimes objects. Link to comment https://forums.phpfreaks.com/topic/49585-solved-moving-to-last-record-mysql/#findComment-243156 Share on other sites More sharing options...
oceans Posted May 2, 2007 Author Share Posted May 2, 2007 Thanks, I see the picture. Link to comment https://forums.phpfreaks.com/topic/49585-solved-moving-to-last-record-mysql/#findComment-243161 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.