Jump to content

Recommended Posts

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);

 

Link to comment
https://forums.phpfreaks.com/topic/240407-last-mysql-table-row/
Share on other sites

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.

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);

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.

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.

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.