PHP處理SQL腳本文件導入到MySQL的代碼實例
來源:易賢網(wǎng) 閱讀:1138 次 日期:2014-08-22 11:08:05
溫馨提示:易賢網(wǎng)小編為您整理了“PHP處理SQL腳本文件導入到MySQL的代碼實例”,方便廣大網(wǎng)友查閱!

通常在制作安裝程式,數(shù)據(jù)備份程序的時候會要用到這樣的代碼,我看網(wǎng)上有是有不太多,而且有些也不是很好用,有時候這種代碼直接用現(xiàn)成的可以節(jié)省很多時間,那么我就從stackoverflow轉(zhuǎn)了一個過來,需要的朋友可以參考下

代碼如下:<?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";

?>

更多信息請查看IT技術專欄

更多信息請查看網(wǎng)絡編程
易賢網(wǎng)手機網(wǎng)站地址:PHP處理SQL腳本文件導入到MySQL的代碼實例
關于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機站點

版權所有:易賢網(wǎng)