你做好程式以後,把資料庫匯出成sql檔案
1、連線資料庫
2、讀取這個sql檔案裡的sql語句,並執行
3、生成一個數據庫連線引數的php檔案
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die("Could not connect: " . mysql_error());
}
if (mysql_query("CREATE DATABASE my_db",$con))
echo "Database created";
else
echo "Error creating database: " . mysql_error();
mysql_close($con);
?>
class ReadSql {
//資料庫連線
protected $connect = null;
//資料庫物件
protected $db = null;
//sql檔案
public $sqlFile = "";
//sql語句集
public $sqlArr = array();
public function __construct($host, $user, $pw, $db_name) {
$host = empty($host) ? C("DB_HOST") : $host;
$user = empty($user) ? C("DB_USER") : $user;
$pw = empty($pw) ? C("DB_PWD") : $pw;
$db_name = empty($db_name) ? C("DB_NAME") : $db_name;
//連線資料庫
$this->connect = mysql_connect($host, $user, $pw) or die("Could not connect: " . mysql_error());
$this->db = mysql_select_db($db_name, $this->connect) or die("Yon can not select the table:" . mysql_error());
//匯入sql檔案
public function Import($url) {
$this->sqlFile = file_get_contents($url);
if (!$this->sqlFile) {
exit("開啟檔案錯誤");
} else {
$this->GetSqlArr();
if ($this->Runsql()) {
return true;
//獲取sql語句陣列
public function GetSqlArr() {
//去除註釋
$str = $this->sqlFile;
$str = preg_replace("/--.*/i", "", $str);
$str = preg_replace("/\/\*.*\*\/(\;)?/i", "", $str);
//去除空格 建立陣列
$str = explode(";\n", $str);
foreach ($str as $v) {
$v = trim($v);
if (empty($v)) {
continue;
$this->sqlArr[] = $v;
//執行sql檔案
public function RunSql() {
foreach ($this->sqlArr as $k => $v) {
if (!mysql_query($v)) {
exit("sql語句錯誤:第" . $k . "行" . mysql_error());
//範例:
header("Content-type:text/html;charset=utf-8");
$sql = new ReadSql("localhost", "root", "", "log_db");
$rst = $sql->Import("./log_db.sql");
if ($rst) {
echo "Success!";
你做好程式以後,把資料庫匯出成sql檔案
1、連線資料庫
2、讀取這個sql檔案裡的sql語句,並執行
3、生成一個數據庫連線引數的php檔案
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die("Could not connect: " . mysql_error());
}
if (mysql_query("CREATE DATABASE my_db",$con))
{
echo "Database created";
}
else
{
echo "Error creating database: " . mysql_error();
}
mysql_close($con);
?>
<?php
class ReadSql {
//資料庫連線
protected $connect = null;
//資料庫物件
protected $db = null;
//sql檔案
public $sqlFile = "";
//sql語句集
public $sqlArr = array();
public function __construct($host, $user, $pw, $db_name) {
$host = empty($host) ? C("DB_HOST") : $host;
$user = empty($user) ? C("DB_USER") : $user;
$pw = empty($pw) ? C("DB_PWD") : $pw;
$db_name = empty($db_name) ? C("DB_NAME") : $db_name;
//連線資料庫
$this->connect = mysql_connect($host, $user, $pw) or die("Could not connect: " . mysql_error());
$this->db = mysql_select_db($db_name, $this->connect) or die("Yon can not select the table:" . mysql_error());
}
//匯入sql檔案
public function Import($url) {
$this->sqlFile = file_get_contents($url);
if (!$this->sqlFile) {
exit("開啟檔案錯誤");
} else {
$this->GetSqlArr();
if ($this->Runsql()) {
return true;
}
}
}
//獲取sql語句陣列
public function GetSqlArr() {
//去除註釋
$str = $this->sqlFile;
$str = preg_replace("/--.*/i", "", $str);
$str = preg_replace("/\/\*.*\*\/(\;)?/i", "", $str);
//去除空格 建立陣列
$str = explode(";\n", $str);
foreach ($str as $v) {
$v = trim($v);
if (empty($v)) {
continue;
} else {
$this->sqlArr[] = $v;
}
}
}
//執行sql檔案
public function RunSql() {
foreach ($this->sqlArr as $k => $v) {
if (!mysql_query($v)) {
exit("sql語句錯誤:第" . $k . "行" . mysql_error());
}
}
return true;
}
}
//範例:
header("Content-type:text/html;charset=utf-8");
$sql = new ReadSql("localhost", "root", "", "log_db");
$rst = $sql->Import("./log_db.sql");
if ($rst) {
echo "Success!";
}
?>