| | |
Importing data from one database to another
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Oct 2006
Posts: 75
Reputation:
Solved Threads: 0
Hi i have a php script that retrieves all data from one database to be transferred to another database. The problem is that the database I am retrieving has 2,778 total rows and after i run my script the new database has 2,658 total rows. Whats going on?
Here is the php code.
Here is the php code.
•
•
•
•
<?php
error_reporting(E_ALL);
/*
First of all, connect to the kmedianet_main table and retrieve all files to be transferred
Uses the PEAR DB class,
*/
require("./DB.php");
$dbun = "example"; // database username
$dbpas = "example"; // database password
$dbname = "example"; // database name
$dsn = "mysql://$dbundbpas@localhost/$dbname";
$conn =& DB::connect($dsn);
if (DB::isError ($conn))
die ("Cannot connect: " . $conn->getDebugInfo () . "\n");
// echo "connected: <Br>";
// echo "got cats: <Br>";
/*
fetch all data from the main table and prepare it for entry into mp3s
*/
$raw_data = array();
$q = 'SELECT users.id, users.username, users.password, users.email, users.ip, users.reg_date, users_profile.fname, users_profile.lname, users_profile.gender, users_profile.birthday, users_profile.country, users_profile.age FROM `users`,`users_profile` WHERE users.id = users_profile.mid';
$r = $conn->query($q);
if(DB::isError($r)) // kill on errors to ensure corrupt data is prevented from entering database
{
die("Error loading from main table: " . $r->getDebugInfo());
}
$x = 0; // initiate incrementer
while($row=$r->fetchRow())
{
// data straight from database
$raw_data[$x]['id'] = $row[0];
$raw_data[$x]['username'] = $row[1];
$raw_data[$x]['password'] = $row[2];
$raw_data[$x]['email'] = $row[3];
$raw_data[$x]['ip'] = $row[4];
$raw_data[$x]['reg_date'] = $row[5];
$raw_data[$x]['fname'] = $row[6];
$raw_data[$x]['lname'] = $row[7];
$raw_data[$x]['gender'] = $row[8];
$raw_data[$x]['birthday'] = $row[9] . " 00:00:00";
$raw_data[$x]['country'] = $row[10];
$raw_data[$x]['age'] = $row[11];
//$raw_data[$x]['reg_date'] = strtotime
$join = strtotime($raw_data[$x]['reg_date']);
$raw_data[$x]['reg_date'] = date("Y-m-d 00:00:00",$join);
$x++; // increase incrementor so that data is not overwritten
}
//echo "<pre>";
//print_r($raw_data);
//echo "</pre>";
// echo "sorted data: <Br>";
/*
connection with the _main database is over. Now connect to the _music database
*/
$dbun = "example"; // database username
$dbpas = "example"; // database password
$dbname = "example"; // database name
$dsn = "mysql://$dbundbpas@localhost/$dbname";
$conn =& DB::connect ($dsn);
if (DB::isError ($conn))
die ("Cannot connect to kmediacom_music: " . $conn->getDebugInfo () . "\n");
// echo "connected to 2nd database: <Br>";
/*
Now add each row of data to the new database
*/
foreach($raw_data as $file)
{
$q = ""; // reset the query string for each iteration
$q = "INSERT IGNORE INTO `users` (user_id, user_name, password, email, first_name, last_name, sex, age, country, usr_status, doj, dob) VALUES ";
$q .= "(";
$q .= "'".$file['id']."',";
$q .= "'".$file['username']."',";
$q .= "'".$file['password']."',";
$q .= "'".$file['email']."',";
$q .= "'".$file['fname']."',";
$q .= "'".$file['lname']."',";
$q .= "'".$file['gender']."',";
$q .= "'".$file['age']."',";
$q .= "'".$file['country']."',";
$q .= "'Ok',";
$q .= "'".$file['reg_date']."',";
$q .= "'".$file['birthday']."'";
$q .= ")"; // construct the INSERT query
$r = $conn->query($q);
//if(DB::isError($r))
// echo "Error inserting into new database on record" . $file['id'] . " : ". $r->getDebugInfo() . "<Br> $q";
}
// echo "inserted data into new database: <Br>";
?>
•
•
Join Date: Aug 2008
Posts: 7
Reputation:
Solved Threads: 0
Because your script is inserting some rows without failing, I would suggest taking a look at your destination table and ensure that the columns are of appropriate length and type to handle the data you are trying to give it. For instance, you may be trying to stuff a varchar(50) into a varchar(40) which would result in the problems you're seeing.
-Andy
-Andy
please also check, is it not problem of time out
Help as an alias
I think programming is great................
Tour Travel weblink by me and about Tour ,
Go To My Home Page and I m in Webdevelopment.
I think programming is great................
Tour Travel weblink by me and about Tour ,
Go To My Home Page and I m in Webdevelopment.
![]() |
Similar Threads
- Importing csv file to SQL Server Using VB.Net (VB.NET)
- How to import Excel Sheet data into SQL Server 2003 ???? Stpes. (MS SQL)
- Warning: mysql_fetch_array (MySQL)
- can't read data files using php inside MAMP (PHP)
- Importing excel data to sql server (C#)
- Importing Data (MySQL)
- Error message while importing data to Excel from the MySQL db (MySQL)
- Please help integrate website and database! (MySQL)
- some help in importing and exporting data (MySQL)
Other Threads in the PHP Forum
- Previous Thread: Day calculation
- Next Thread: Webdesigner to PHP Web Developer
Views: 1270 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for PHP
.htaccess access address ajax apache api array autoincrement beginner binary broken cakephp checkbox class cms code cron curl database date dehasher directory display download dynamic echo email error file files folder form forms function functions google href htaccess html image include insert integration ip java javascript joomla jquery limit link login loop mail menu method methods mlm mod_rewrite multiple mysql oop parse paypal pdf php problem query radio random recursion regex remote script search select server sessions sms soap source space speed sql structure support! syntax system table tutorial update updates upload url validation validator variable video web xml youtube




dbpas@localhost/$dbname";
