PHP處理SQL腳本文件導(dǎo)入到MySQL的代碼實(shí)例
更新時(shí)間:2014年03月17日 13:50:12 作者:
通常在制作安裝程式,數(shù)據(jù)備份程序的時(shí)候會(huì)要用到這樣的代碼,我看網(wǎng)上有是有不太多,而且有些也不是很好用,有時(shí)候這種代碼直接用現(xiàn)成的可以節(jié)省很多時(shí)間,那么我就從stackoverflow轉(zhuǎn)了一個(gè)過(guò)來(lái),需要的朋友可以參考下
復(fù)制代碼 代碼如下:
<?php
// Name of the file
$filename = 'churc.sql';
// MySQL host
$mysql_host = 'localhost';
// MySQL username
$mysql_username = 'root';
// MySQL password
$mysql_password = '';
// Database name
$mysql_database = 'dump';
// Connect to MySQL server
mysql_connect($mysql_host, $mysql_username, $mysql_password) or die('Error connecting to MySQL server: ' . mysql_error());
// Select database
mysql_select_db($mysql_database) or die('Error selecting MySQL database: ' . mysql_error());
// Temporary variable, used to store current query
$templine = '';
// Read in entire file
$lines = file($filename);
// Loop through each line
foreach ($lines as $line)
{
// Skip it if it's a comment
if (substr($line, 0, 2) == '--' || $line == '')
continue;
// Add this line to the current segment
$templine .= $line;
// If it has a semicolon at the end, it's the end of the query
if (substr(trim($line), -1, 1) == ';')
{
// Perform the query
mysql_query($templine) or print('Error performing query \'<strong>' . $templine . '\': ' . mysql_error() . '<br /><br />');
// Reset temp variable to empty
$templine = '';
}
}
echo "Tables imported successfully";
?>
// Name of the file
$filename = 'churc.sql';
// MySQL host
$mysql_host = 'localhost';
// MySQL username
$mysql_username = 'root';
// MySQL password
$mysql_password = '';
// Database name
$mysql_database = 'dump';
// Connect to MySQL server
mysql_connect($mysql_host, $mysql_username, $mysql_password) or die('Error connecting to MySQL server: ' . mysql_error());
// Select database
mysql_select_db($mysql_database) or die('Error selecting MySQL database: ' . mysql_error());
// Temporary variable, used to store current query
$templine = '';
// Read in entire file
$lines = file($filename);
// Loop through each line
foreach ($lines as $line)
{
// Skip it if it's a comment
if (substr($line, 0, 2) == '--' || $line == '')
continue;
// Add this line to the current segment
$templine .= $line;
// If it has a semicolon at the end, it's the end of the query
if (substr(trim($line), -1, 1) == ';')
{
// Perform the query
mysql_query($templine) or print('Error performing query \'<strong>' . $templine . '\': ' . mysql_error() . '<br /><br />');
// Reset temp variable to empty
$templine = '';
}
}
echo "Tables imported successfully";
?>
相關(guān)文章
php版微信開(kāi)發(fā)Token驗(yàn)證失敗或請(qǐng)求URL超時(shí)問(wèn)題的解決方法
這篇文章主要介紹了php版微信開(kāi)發(fā)Token驗(yàn)證失敗或請(qǐng)求URL超時(shí)問(wèn)題的解決方法,簡(jiǎn)單分析了Token驗(yàn)證失敗及請(qǐng)求URL超時(shí)的原因及相關(guān)解決方法,需要的朋友可以參考下2016-09-09
一文掌握PHP Xdebug 本地與遠(yuǎn)程調(diào)試(小結(jié))
這篇文章主要介紹了一文掌握PHP Xdebug 本地與遠(yuǎn)程調(diào)試(小結(jié)),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-04-04
php實(shí)現(xiàn)留言板功能(會(huì)話控制)
這篇文章主要為大家詳細(xì)介紹了php實(shí)現(xiàn)留言板功能,會(huì)話控制的案例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05
php封裝好的人民幣數(shù)值轉(zhuǎn)中文大寫(xiě)類
這篇文章主要給大家分享了幾個(gè)php實(shí)現(xiàn)的人民幣數(shù)值轉(zhuǎn)中文大寫(xiě)的代碼,非常的全面,有需要的小伙伴可以查看下2015-12-12
PHP unlink與rmdir刪除目錄及目錄下所有文件實(shí)例代碼
這篇文章主要介紹了PHP unlink與rmdir刪除目錄及目錄下所有文件的實(shí)例代碼,需要的朋友可以參考下2018-02-02
laravel 實(shí)現(xiàn)劃分admin和home 模塊分組
今天小編就為大家分享一篇laravel 實(shí)現(xiàn)劃分admin和home 模塊分組,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10
PHP中的靜態(tài)變量及static靜態(tài)變量使用詳解
這篇文章主要 紹了PHP中的靜態(tài)變量及static靜態(tài)變量使用詳解的相關(guān)資料,需要的朋友可以參考下2015-11-11

