Jump to content

[SOLVED] MySQL id numbers


Andy17

Recommended Posts

Hey again,

 

Let's say I have a MySQL table with id 0, 1, 2, 3, 4, 5 and I delete the rows with id=2, 3. This would leave me with the rows with id=0, 1, 4, 5. Then the next time I add content to my table, the new row's id will automatically be 6, even though some previous rows were deleted. Is there are way I can make it "fill out" the missing rows? I mean, so the next two rows would be with id=2, 3 instead of 5, 6; so there are no "spaces"?

 

I hope you get my point.

Link to comment
Share on other sites

Well, it just caused me a small problem in my "random joke" script. I have the total number of rows set to $maxrows and I generate a number between 1 and $maxrows. That generated number is the id of the row I want to show the content of. So, for example, I have a table with 10 rows, then it will generate a number from 1-10. If the number is 4, then the row with id=4 will be shown. However, if that row is removed, nothing will be shown.

 

Here is some of the code:

 

<?php

$number = rand(1, $maxrows);

$sql1 = "SELECT * FROM jokes WHERE id = '$number'";
$result = mysql_query($sql1);

if ($result)

{

$row = mysql_fetch_array($result);

   // Echoing the content here

}

?>

 

Any suggestions of a better way to do it to avoid the id gap problem?

 

Thank you.

Link to comment
Share on other sites

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.