如何实现PHP自动创建数据库
最近写了一个企业网站,感觉导入数据库很麻烦,想知道如何实现自动创建、导入数据库。如果可以的话,最好说一下具体思路和大概参考代码。再次,先表示感谢了。...
最近写了一个企业网站,感觉导入数据库很麻烦,想知道如何实现自动创建、导入数据库。如果可以的话,最好说一下具体思路和大概参考代码。再次,先表示感谢了。
展开
3个回答
展开全部
你做好程序以后,把数据库导出成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!";
}
?>
展开全部
你做好程序以后,把数据库导出成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!";
}
?>
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!";
}
?>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这个应该涉及到怎么使用PHP和Mysql命令行窗口交互了~~太底层了!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询