Jump to content

for($i=0;$i<$rownum;$i++)


Miss-Ruth

Recommended Posts

I need this script to go through each row of the mysql db and update the cat_num colum (for each record) with the new value. But this doen't seem to work. 

 

$marks = '25'; // this value is ever changing. For this example I'll put as 25

$query=mysql_query("SELECT * FROM table1");
$rownum=mysql_num_rows($query);
for($i=0;$i<$rownum;$i++)
{
    $query=mysql_query("SELECT * FROM table1 LIMIT $i,1");
    while($select=mysql_fetch_array($query))
    {
         $update=mysql_query("UPDATE table2 SET cat_num='$select[cat_num]'+$marks");
    }
}

Link to comment
Share on other sites

$query=mysql_query("SELECT * FROM table1");

$rownum=mysql_num_rows($query); // you are fetching all records from table1

 

for($i=0;$i<$rownum;$i++)

{

    $query=mysql_query("SELECT * FROM table1 LIMIT $i,1"); //why this is required here? you already fetched records from table1

    while($select=mysql_fetch_array($query))

    {

        $update=mysql_query("UPDATE table2 SET cat_num='$select[cat_num]'+$marks"); // print the update query

    }

}

Link to comment
Share on other sites

$marks = '25'; // this value is ever changing. For this example I'll put as 25

$query='SELECT * FROM table1';
$result=mysql_query($query);
while($row=mysql_fetch_array($result)){
mysql_query('UPDATE table2 SET cat_num='.$row[cat_num]+$marks);
}

You may want to put the query string inside the actual mysql_query function, but don't be tempted to put the mysql_query inside the mysql_fetch_array while loop. It will then loop get a new mysql_query function each time, infinite, unless there is some remove query.

 

I don't see why you need 25..., do you need to do it max 25 times?

Because if so, just put a check at the end after the update query:

$marks--;
if($marks==0){
break;
}

 

 

$rownum=mysql_num_rows($query);

I see why you want to use the number of rows, but I don't see why you actually need it...

 

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.