Jump to content

[SOLVED] Help with code (php/mysql) move data from one column to another


lindm

Recommended Posts

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);



?>

 

 

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);



?>

 

 

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);



?>

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.