function my_copy_table($table_name) { $dbIN = my_MySQL_db_connect(); $sql = "SELECT * FROM {$table_name}"; $resIN = mysql_query($sql); if (!$resIN) { echo "Could not successfully run query ({$sql}) from DB: ", mysql_error(), "\n"; return; } if (mysql_num_rows($resIN) == 0) { echo "No rows in {$table_name} found.\n"; return; } $dbOUT = my_Sqlite_db_connect(); $i = 0; echo "\n"; while ($row = mysql_fetch_assoc($resIN)) { $columns = ''; $values = ''; //print_r($row); exit; // debug foreach ($row as $key => $value) { //echo "$key = $value\n"; // debug $columns = $columns . $key . ' ,'; $values .= $dbOUT->quote($value) . ' ,'; } $columns = rtrim($columns, ','); $values = rtrim($values, ','); unset($row); // http://by.php.net/manual/en/book.pdo.php $query = "INSERT INTO {$table_name} ({$columns}) VALUES ({$values})"; //echo $query, "\n"; $resOUT = $dbOUT->query($query); if (!$resOUT) { echo "\n\nERROR : array([0] SQLSTATE error code, [1] Driver-specific error code, [2] Driver-specific error message)\n"; print_r($dbOUT->errorInfo()); echo "\n\n"; echo "SQL QUERY : {$query} \n\n"; exit; } $resOUT->closeCursor(); unset($query); unset($resOUT); $i++; //echo "."; // progress bar } // close MySQL if ($resIN) { mysql_free_result($resIN); } if ($dbIN) { mysql_close($dbIN); } // close Sqlite unset($queryOUT); unset($resOUT); unset($dbOUT); echo "Table : '{$table_name}' , insert ", $i, " row(s)\n"; }
function my_copy_table($table_name) { $dbIN = my_MySQL_db_connect(); $dbOUT = my_PGSQL_db_connect(); $sql = "SELECT * FROM {$table_name}"; $resIN = mysql_query($sql); if (!$resIN) { echo "Could not successfully run query ({$sql}) from DB: ", mysql_error(), "\n"; return; } if (mysql_num_rows($resIN) == 0) { echo "No rows found, exiting.\n"; return; } $i = 0; while ($row = mysql_fetch_assoc($resIN)) { $columns = ''; $values = ''; //print_r($row); exit; // debug foreach ($row as $key => $value) { //echo "$key = $value\n"; // debug $columns = $columns . $key . ','; if ($value == '0000-00-00 00:00:00') { $values .= "NULL,"; } else { $values .= "'" . pg_escape_string($dbOUT, $value) . "',"; } } $columns = rtrim($columns, ','); $values = rtrim($values, ','); unset($row); $query = "INSERT INTO {$table_name} ({$columns}) VALUES ({$values})"; //echo $query, "\n"; $resOUT = pg_query($dbOUT, $query); if (!$resOUT) { die("\n\nSQL : {$query}\n\n"); } $i++; } // close MySQL if ($resIN) { mysql_free_result($resIN); } if ($dbIN) { mysql_close($dbIN); } // close PGSQL if ($resOUT) { pg_free_result($resOUT); } if ($dbOUT) { pg_close($dbOUT); } echo "Table : '{$table_name}' , insert ", $i, " row(s)\n"; }