Updating mySql database in for loop

Hi to all… I am new to this forum. It seems that everyone is really helpful so I thought this is where I would sign up. I would greatly appreciate any help on the following. I have distilled the code down to where I am having a problem. The paragraph below explains the total project which you can skip if you want.

I am teaching myself PHP ad mySql and as an exercise decided I would create a page that would update a database just like Netflix. At Netflix your home page shows all the movies that you have in your queue in order of how they will be sent to you. You can change the order of your movies by entering a number by the movie you want to move in your queue. In other words if you want a movie that was #10 on your list to be moved to #1, you enter a number 1 by that move and it moves up to the first spot when the you update the page.

The overall code is a bit complicated (for me), but I have narrowed down my problem to the following code. It’s such a simple thing but for some reason I cannot get it to run properly.

PHP Code:


<?php
$con 
mysql_connect(“localhost”,“root”,“*******”);
if (!
$con)  {
  die(
‘Could not connect to local host: ’ mysql_error());
  }
mysql_select_db(“items”$con);
$result mysql_query(“SELECT * FROM isc_hat_items ORDER BY man_sort_num ”);
$num_rows mysql_num_rows($result);
for(
$i=0$i<$num_rows$i++):        
                 
//advances the row in the mysql resource $row     mysql_data_seek($result,$i);
    
$rowsmysql_fetch_row($result);
    echo 
“The counter is ”.$i;
echo 
“<pre>”
    
print_r($rows);
    echo 
“</pre>”;
mysql_query(“UPDATE isc_hat_items SET man_sort_num =”$i);
endfor;



The counter ($i) increments properly but the sort field I am updating in the database posts as 32 in each row. I have 34 records in the database. It seems like truly bizare behavior.

Leave a Reply