lindm Posted July 18, 2008 Share Posted July 18, 2008 I need help with a script that performs the following: 1. Moves the contents from columns %cy to columns %py Basic starting points: 1. Table contains different columns but those ending with %cy have a "pair column" ending with %py Link to comment https://forums.phpfreaks.com/topic/115379-solved-help-with-code-phpmysql-move-data-from-one-column-to-another/ Share on other sites More sharing options...
lindm Posted July 18, 2008 Author Share Posted July 18, 2008 Here is my current status. Stuck on the array part. Any help appreciated: <?php include ('mysqlconnnection.php'); //include connection //gets all data $resultCY = mysql_query("SELECT * FROM $table") or die("mysql error"); //retrieves columns ending with cy $rowCY = mysql_fetch_array($resultCY); //find all columns ending with py $resultPY = mysql_query("SHOW COLUMNS FROM $table LIKE '%py'") or die("mysql error"); //retrieves columns ending with py $numColumns = mysql_num_rows($resultPY); //gets number of rows ending with PY //creates an array where columns XXXXpy=value of XXXXcy. STUCK here. $x = 0; while ($x < $numColumns) { $colname = mysql_fetch_row($resultPY); $col[$colname[0]] = str_replace("py", "cy", $row[$colname[0]]); $x++; } $querycx = 'UPDATE '.$table.' SET '. implode( '=\'\', ', $col ) .'=\'\''; //new array is used in mysql query mysql_query($querycx); ?> Link to comment https://forums.phpfreaks.com/topic/115379-solved-help-with-code-phpmysql-move-data-from-one-column-to-another/#findComment-593527 Share on other sites More sharing options...
lindm Posted July 18, 2008 Author Share Posted July 18, 2008 Corrected the array now. Silly fault. Array now is for instance: Array ( [RRintNOpy] => 100000 [RRintFORLAGpy] => 0 [RRintAKTARBpy] => 0) Now just need to create the mysql_query... <?php include ('mysqlconnnection.php'); //include connection //gets all data $resultCY = mysql_query("SELECT * FROM $table") or die("mysql error"); //retrieves columns ending with cy $rowCY = mysql_fetch_array($resultCY); //find all columns ending with py $resultPY = mysql_query("SHOW COLUMNS FROM $table LIKE '%py'") or die("mysql error"); //retrieves columns ending with py $numColumns = mysql_num_rows($resultPY); //gets number of rows ending with PY //creates an array where columns XXXXpy=value of XXXXcy. STUCK here. $x = 0; while ($x < $numColumns) { $colname = mysql_fetch_row($resultPY); $col[$colname[0]] = $rowCY[str_replace("py", "cy", $colname[0])]; $x++; } $querycx = 'UPDATE '.$table.' SET '. implode( '=\'\', ', $col ) .'=\'\''; //new array is used in mysql query mysql_query($querycx); ?> Link to comment https://forums.phpfreaks.com/topic/115379-solved-help-with-code-phpmysql-move-data-from-one-column-to-another/#findComment-593537 Share on other sites More sharing options...
lindm Posted July 18, 2008 Author Share Posted July 18, 2008 Solved it <?php include ('mysqlconnnection.php'); //include connection //gets all data $resultCY = mysql_query("SELECT * FROM $table") or die("mysql error"); //retrieves columns ending with cy $rowCY = mysql_fetch_array($resultCY); //find all columns ending with py $resultPY = mysql_query("SHOW COLUMNS FROM $table LIKE '%py'") or die("mysql error"); //retrieves columns ending with py $numColumns = mysql_num_rows($resultPY); //gets number of rows ending with PY //creates an array where columns XXXXpy=value of XXXXcy. STUCK here. $x = 0; while ($x < $numColumns) { $colname = mysql_fetch_row($resultPY); $col[$colname[0]] = $colname[0]."='".$rowCY[str_replace("py", "cy", $colname[0])]."'"; $x++; } //print_r($col); $query = 'UPDATE '.$table.' SET '. implode( ', ', $col ) ; //new array is used in mysql query //echo $query; //exit; mysql_query($query); ?> Link to comment https://forums.phpfreaks.com/topic/115379-solved-help-with-code-phpmysql-move-data-from-one-column-to-another/#findComment-593662 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.