perryeb Posted June 25, 2011 Share Posted June 25, 2011 How do I find the last table row id, and put it into the second field of mt_rand this following clearly isn't working. Thank you very much. $totalrows = mysql_query("SELECT id * FROM `business`"); $gettotalrows = mysql_num_rows($totalrows) OR die('Error pulling total database rows: ' . mysql_error()); $rand = mt_rand(0, $gettotalrows); Quote Link to comment https://forums.phpfreaks.com/topic/240407-last-mysql-table-row/ Share on other sites More sharing options...
wildteen88 Posted June 25, 2011 Share Posted June 25, 2011 Are you trying to select a random row from your database. You can do this within your query SELECT * FROM table ORDER BY rand() LIMIT 1 The above query will select one random row from the table. Change Limit 1 to the number of random rows you want the query to return. Quote Link to comment https://forums.phpfreaks.com/topic/240407-last-mysql-table-row/#findComment-1234823 Share on other sites More sharing options...
perryeb Posted June 25, 2011 Author Share Posted June 25, 2011 Thank you very much but coders usually have a reason for asking something, why that won't work. I need to know the last rows id that is in a table, and then put that last rows id into the second field of mt_rand. $rand = mt_rand(0, ?); Quote Link to comment https://forums.phpfreaks.com/topic/240407-last-mysql-table-row/#findComment-1234824 Share on other sites More sharing options...
Pikachu2000 Posted June 25, 2011 Share Posted June 25, 2011 How about SELECT MAX(id)? Quote Link to comment https://forums.phpfreaks.com/topic/240407-last-mysql-table-row/#findComment-1234825 Share on other sites More sharing options...
perryeb Posted June 25, 2011 Author Share Posted June 25, 2011 Ding, Ding, Ding you win!!!!!!!!!!!!!! Thanks a lot that was it!!!!! $lastrow = mysql_query("SELECT SELECT MAX(id) FROM `business`"); $rand = mt_rand(1,$lastrow); Quote Link to comment https://forums.phpfreaks.com/topic/240407-last-mysql-table-row/#findComment-1234827 Share on other sites More sharing options...
wildteen88 Posted June 25, 2011 Share Posted June 25, 2011 Thank you very much but coders usually have a reason for asking something I was just assuming you was going to be using mt_rand to select a random row from your table latter on in your script. That why I mentioned you can select random rows from within your query it self. I need to know the last rows id that is in a table To get the last row you use a query like this SELECT id FROM `business` ORDER BY id DESC LIMIT 1 that query will get the last row from the business table and then put that last rows id into the second field of mt_rand. Something like this should work $result = mysql_query("SELECT id FROM `business` ORDER BY id DESC LIMIT 1"); list($last_row_id) = mysql_fetch_row($result); $rand = mt_rand(0, $last_row_id); Quote Link to comment https://forums.phpfreaks.com/topic/240407-last-mysql-table-row/#findComment-1234828 Share on other sites More sharing options...
perryeb Posted June 25, 2011 Author Share Posted June 25, 2011 I figured that's what you thought I was just doing thanks for trying to help. Yes those will both work that same exact way! Quote Link to comment https://forums.phpfreaks.com/topic/240407-last-mysql-table-row/#findComment-1234830 Share on other sites More sharing options...
perryeb Posted June 25, 2011 Author Share Posted June 25, 2011 While any good random guys see this $lastrow = mysql_query("SELECT SELECT MAX(id) FROM `business`"); $rand = mt_rand(1,$lastrow); mysql_query("SELECT id FROM business WHERE id>=1 ORDER BY $rand LIMIT 5")OR die can you tell me how to mix up the 5 rows at a time randomly? so if 11 total rows it would be rows 5, 10, 1, 11, 3 or whatever on one refresh. Then all mixed up numbers to 11, with another 5 at a time on another refresh. All what I just listed is doing is pulling 5 rows in order randomly. So this code I just quoted is pulling rows 7, 8, 9, 10, 11, 12 on one refresh, and rows 1, 2, 3, 4, 5 or whatever on another refresh. So this is doing 5 at a time randomly but is putting the row id numbers in order. Where I want 5 at a time but like the first example I just said instead, I want the 5 ids pulled not in order, but all mixed up. Quote Link to comment https://forums.phpfreaks.com/topic/240407-last-mysql-table-row/#findComment-1234833 Share on other sites More sharing options...
perryeb Posted June 25, 2011 Author Share Posted June 25, 2011 isset($_COOKIE['a']) ?: setcookie("a", 1, time()+60*60*24*730); $a=$_COOKIE["a"];} ELSE{$a=1;} $lastrow = mysql_query("SELECT SELECT MAX(id) FROM `business`"); $rand = mt_rand(1,$lastrow); mysql_query("SELECT id FROM business WHERE id>= $a ORDER BY $rand LIMIT 5")OR die(': ' . mysql_error()); Is it the 'ORDER BY' ? How do I write this mysql_query("SELECT id FROM business WHERE id>= $a ORDER BY $rand LIMIT 5")OR die(': ' . mysql_error()); So that it mixed the 5 id rows all up instead? Just removing 'ORDER BY' from it doesn't work. Quote Link to comment https://forums.phpfreaks.com/topic/240407-last-mysql-table-row/#findComment-1234840 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.