PHP import. sql files to MySQL database

Source: Internet
Author: User
Tags chr php and strlen
The code is as follows Copy Code

Set_time_limit (0); The timeout is set to 0, which indicates that it has been executed. When PHP is invalid in safe Mode mode, this may cause the import timeout, which requires a segmented import
$db = new MySQL ($location [' Host '], $location [' hostname '], $location [' Hostpass '], $location [' table '], ' UTF8 ', $location [' Ztime ']);
$fp = @fopen ($sql, "R") or Die ("Cannot open SQL file $sql");/Open File
while ($sql =getnextsql ()) {
mysql_query ($sql);
}
echo "User data import complete!";
Fclose ($fp) or Die ("can ' t close file $file _name");

Take SQL from a file
function Getnextsql () {
Global $fp;
$sql = "";
while ($line = @fgets ($fp, 40960)) {
$line = Trim ($line);
The following three sentences are not required in a high version of PHP and may need to be modified in some low versions
$line = Str_replace ("\", "\", $line);
$line = Str_replace ("'", "'", $line);
$line = Str_replace ("\ r \ n", Chr. Chr (a), $line);
$line = Stripcslashes ($line);
if (strlen ($line) >1) {
if ($line [0]== "-" && $line [1]== "-") {
Continue
}
}
$sql. = $line. chr. chr (10);
if (strlen ($line) >0) {
if ($line [strlen ($line) -1]== ";") {
Break
}
}
}
return $sql;
}
?>

Related Article

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.