Current location: Hot Scripts Forums » Programming Languages » PHP » Update multiple rows outside loop - need help


Update multiple rows outside loop - need help

Reply
  #1 (permalink)  
Old 12-02-06, 04:24 PM
ElvansX ElvansX is offline
New Member
 
Join Date: Dec 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Update multiple rows outside loop - need help

I know it possible to update rows in loop. But how about outside the loop?
Normally i update in loop like this:

PHP Code:

  $query "SELECT id, title FROM table1 ORDER BY id ASC";

  
$result mysql_query($query) or die(mysql_error());  
  while(
$row mysql_fetch_array($resultMYSQL_ASSOC)) {
    
$id $row['id'];
    
$title $row['title'];

    
$title FUNCTION_CHG_TITLE_VALUE_HERE($title);

    
$sql_update "UPDATE table SET title = '$title' WHERE id=$id";
    
mysql_query($sql_update) or die(mysql_error());
  } 
This works just fine. Outside loop:
PHP Code:

   $query "SELECT id, title FROM table ORDER BY id ASC";

  
$result mysql_query($query) or die(mysql_error());
  
$all_sql_update"";
  while(
$row mysql_fetch_array($resultMYSQL_ASSOC)) {
    
$id $row['id'];
    
$title $row['title'];

    
$title FUNCTION_CHG_TITLE_VALUE_HERE($title);

    
// Update current DB
    
$sql_update "UPDATE table SET title = '$title' WHERE id=$id";
    
mysql_query($sql_update) or die(mysql_error());
    
    
// if i put function to update DB on other hosting here working, but slow or time out.
    // Function: connect other hosting db, select db, update, dc  
    // FUNCTION_UPDATE_OTHER_DB($sql_update);

    // Collect all update
    
$all_sql_update"$all_sql_update $sql_update";
  }
  
   
// Not working :
   
FUNCTION_UPDATE_OTHER_DB($all_sql_update); 
This one wont work!
I tried put each update ending with a comma/semicolon (like insert), not working!

Actually i need this to update other DB on other hosting. If i put the Update Function to update DB on other hosting in loop it wil works, but took long times to update each of it.

Connnect -> look for DB -> update row -> disconnect

for every rows. The database is not big, but if too much rows to update it will take long time or time out.
I did for multi insert records, well that easy.

Help anyone?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 12-03-06, 02:55 AM
ElvansX ElvansX is offline
New Member
 
Join Date: Dec 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Solved this problem myself :lol:

I create another function similar to FUNCTION_UPDATE_OTHER_DB, add code to count the records to update, then update 1 by 1. Like this

PHP Code:

 $query "SELECT id, title FROM table ORDER BY id ASC";

  
$result mysql_query($query) or die(mysql_error());
  
$all_sql_update"";
  while(
$row mysql_fetch_array($resultMYSQL_ASSOC)) {
    
$id $row['id'];
    
$title $row['title'];

    
$title CUSTOM_FUNCTION($title); // change title's value

    
$sql_update "UPDATE table SET title = '$title' WHERE id=$id";
    
mysql_query($sql_update) or die(mysql_error());
        
    
// Collect all update
    
$all_sql_update"$all_sql_update $sql_update;";
  }
  
   
// Working now!
   
FUNCTION_UPDATE_MANY_FOR_OTHER_DB($all_sql_update); 
this is important:
PHP Code:

$all_sql_update"$all_sql_update $sql_update;"
put semicolon to seperate each update records. Make sure ur update didnt use any semicolon or else replace with something else.

then the function is something like this:

PHP Code:

function FUNCTION_UPDATE_MANY_FOR_OTHER_DB($sql_query) {

    
    
/// Count how many records to update ///
    
$explode_sql_query explode(";"$sql_query);
    
$total_sql_query count($explode_sql_query);

    
/// Connect other host script here ///
   
    
for ($i=0$i<$total_sql_query$i++) {
      
mysql_query($explode_sql_query[$i]) or die(mysql_error());
    } 

    
/// Close connection here ///

* If you believed in your programming, you will always find answer for your problem!
Hope this will helps someone else
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
inserting multiple rows into mysql ! n3wb!e PHP 12 04-28-06 08:24 AM
UPDATE mysql from multiple POST rows wwanthony PHP 10 01-27-06 05:39 AM
Update multiple row...Loop prince PHP 0 01-18-06 03:54 AM
can't edit multiple rows at the same time conundrum PHP 2 04-01-04 01:50 AM
type mismatch and update loop - HELP! seala ASP 1 09-22-03 06:27 PM


All times are GMT -5. The time now is 01:21 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.