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)文章
tp5.1 實(shí)現(xiàn)setInc字段自動(dòng)加1
今天小編就為大家分享一篇tp5.1 實(shí)現(xiàn)setInc字段自動(dòng)加1示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10PHP 提取圖片img標(biāo)記中的任意屬性的簡(jiǎn)單實(shí)例
這篇文章主要介紹了PHP 提取圖片img標(biāo)記中的任意屬性的簡(jiǎn)單實(shí)例,有需要的朋友可以參考一下2013-12-12PHP使用GIFEncoder類處理gif圖片實(shí)例
這篇文章主要介紹了PHP使用GIFEncoder類處理gif圖片實(shí)例,包含解碼GIF圖片、處理后再次生成GIF圖片例子,需要的朋友可以參考下2014-07-07Yii框架實(shí)現(xiàn)記錄日志到自定義文件的方法
這篇文章主要介紹了Yii框架實(shí)現(xiàn)記錄日志到自定義文件的方法,結(jié)合實(shí)例形式分析了Yii框架日志記錄的原理及自定義日志記錄的相關(guān)配置與實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-05-05