looping through MySQL to change the id to equal # of rows
Posted: Tue Oct 07, 2003 11:09 pm
When deleting a row from the database, that id is now missing.
-So what I'm trying to do, is update in a loop of all of the
id numbers - sort of like renaming them.
Here's the code I used:
$x=0;
$query1 = mysql_query ("SELECT * FROM counter", $db);
$thenum = mysql_num_rows($query1);
while( $therow = mysql_fetch_array($thenum) )
{
$x++;
$therow_id = $therow[id];
$query2 = "UPDATE counter SET id='$x' WHERE id='$therow_id' limit 1";
$aResult = mysql_query($query2);
}
Nothing happens though and I get this:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result
that warning can't be right because I've used it before in another place.
Any-ways I'm probably missing something in the code.
-So what I'm trying to do, is update in a loop of all of the
id numbers - sort of like renaming them.
Here's the code I used:
$x=0;
$query1 = mysql_query ("SELECT * FROM counter", $db);
$thenum = mysql_num_rows($query1);
while( $therow = mysql_fetch_array($thenum) )
{
$x++;
$therow_id = $therow[id];
$query2 = "UPDATE counter SET id='$x' WHERE id='$therow_id' limit 1";
$aResult = mysql_query($query2);
}
Nothing happens though and I get this:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result
that warning can't be right because I've used it before in another place.
Any-ways I'm probably missing something in the code.