Import Export a large number of data program implementation method

Source: Internet
Author: User
Tags exit chop file size ftp mysql query phpmyadmin
We must have used phpMyAdmin inside the database import, export function, very convenient. But in practical applications, I found the following questions:

1 database more than a certain size, such as 6M at this time using the export generally no problem, you can correctly save to the local hard disk, but the import is not! The reason is: General PHP. INI set temporary file/upload file size limit of 2M, and phpMyAdmin use the way to upload, resulting in failure.

2 exported to the hard drive. SQL file in the lead back, often because of some single quotes problems caused by failure, resulting in the import failure, can only use MySQL and other applications imported. My database is over 10M, so this problem must be addressed. My train of thought:

Export: Save database/table structure with phpMyAdmin, read database content with script and save to file!
Import: Restore the database/table structure with phpMyAdmin, read the file with the script, and save it to the library!

The export process is as follows: Call method ****.php?table=tablename
This simple program saves a table at once!! Each behavior a field of data!!

The
The following is the referenced content:
<?
if ($table ==\ "\") exit ();
mysql_connect (\ "localhost\", \ "name\", \ "password\");
mysql_select_db (\ "database\");
$result = mysql_query (\ "SELECT * from $table \");
if (mysql_num_rows ($result) < = 0) exit ();
Echo \ Begins converting data to text ...
\ ";
$handle = fopen (\ "$table. txt\", \ "w\");
$numfields = Mysql_num_fields ($result);
Fputs ($handle, $numfields. \ "\r\n\");
for ($k =0; $k < mysql_num_rows ($result); $k + +)
{
$msg = mysql_fetch_row ($result);
for ($i =0; $i < $numfields; $i + +)
{
$msg [$i] = Str_replace (\ \r\n\, \ &&php2000mysqlreturn& &\ ", $msg [$i]);
$msg [$i] = Str_replace (\ "\n\", \ "&&php2000mysqlreturn&&\", $msg [$i]);
Fputs ($handle, $msg [$i].\ "\r\n\");
}
fputs ($handle, \ "-------php2000 dump Data program V1.0 for MySQL--------\r\n\");
}
Fclose ($handle);
echo \ "Ok\";
?

The imported programs are as follows: Use the same top!
The following are the referenced contents:
<?
if ($table ==\ "\") exit ();
Mysql_connect ("localhost\", "name\", "password\");
mysql_select_db (\ "database\");
$message = file (\ "$table. txt\");
echo $numfields = Chop ($message [0]);
For ($k =1 $k < count ($message); $k + + ($numfields + 1))
{
$value =\ "\";
for ($i = $k; $i < ($k + $numfields-1); $i + +)
{
$tmp = Str_replace (\ "&&php2000mysqlreturn&&\", \ "\r\n\", Chop ($message [$i]));
$value. = \ "\ \". Addslashes ($tmp). \ "\ ', \";
}
$tmp = Str_replace (\ "&&php2000mysqlreturn&&\", \ "\r\n\", Chop ($message [$k + $numfields-1]);
$value. = \ "\ \". $tmp. \ "\";
$query = \ INSERT into $table values (\. $value. \ ") \";
Echo Mysql_error ();
mysql_query ($query);
echo $k. \ "\";
}
echo \ "Ok\";
?>
How to use and possible problems!
1 the file () function may have problems while importing (my 10M data is not a problem), you can change to fopen () and not read one line!!
2 Import, export all need to use FTP operation, that is, after the export, with FTP data to the local machine, the first transfer data to the server with FTP!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.