Jump to content
Pauls74462

Id reset

Recommended Posts

I have a db and when I delete a row then my Id numbers get out of sink. How to I keep the rows in numeral order after delete of a rod have been done?I need the Id to be in numerical so I can do a random function.Paul

Share this post


Link to post
Share on other sites

that's just how it works. if you're trying to get random information just check to make there's data being returned (which you should be checking for anyway) from the random id picked, if there isn't then just run the random function again.

Edited by thescientist

Share this post


Link to post
Share on other sites
that's just how it works. if you're trying to get random information just check to make there's data being returned (which you should be checking for anyway) from the random id picked, if there isn't then just run the random function again.
Here is how I choose a random users, is there a better way?
$result = mysql_query("SELECT * FROM MyTable"); $Choose_rows = mysql_num_rows($result); $Num2Use = rand(1, $Choose_rows);$result = mysql_query("SELECT * FROM Members where id = $Num2Use");

Edited by Pauls74462

Share this post


Link to post
Share on other sites

if it gives you the random result number that suits your needs then yes. My only point is you should be checking for the data being returned by the query in the event the id doesn't exist.

Share this post


Link to post
Share on other sites

say you have a 100 records, you can't guarantee that the id ref is between 1-100, could be 1 to whatever.I found this, i don't know if it will work for youmysql_query("SELECT * FROM Members WHERE RAND() ORDER BY RAND() LIMIT 1");other optionAdd the id ref to an array, get random number as you have, then the random number will relate to the id ref position in the array.

Edited by dsonesuk

Share this post


Link to post
Share on other sites
say you have a 100 records, you can't guarantee that the id ref is between 1-100, could be 1 to whatever.I found this, i don't know if it will work for youmysql_query("SELECT * FROM Members WHERE RAND() ORDER BY RAND() LIMIT 1");other optionAdd the id ref to an array, get random number as you have, then the random number will relate to the id ref position in the array.
The array sounds like a good option, but what is the code for it?

Share this post


Link to post
Share on other sites

code is to look it up.mt_rand() for random number.how to create arrayswhile loop to store record id values in arrayproduce a sql query that will match the id ref in $id_array[$Num2Use]another while loop to show selected random record

Share this post


Link to post
Share on other sites

For small data sets, the ORDER BY RAND() method is probably the easiest:

SELECT * FROM Members ORDER BY RAND() LIMIT 1

Otherwise, you may need to look at another method, like dsonesuk's.

Share this post


Link to post
Share on other sites

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.   Paste as plain text instead

  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.

Loading...

×
×
  • Create New...