你做好程序以后,把數(shù)據(jù)庫(kù)導(dǎo)出成sql文件(這個(gè)文件里就已經(jīng)有了一下創(chuàng)建數(shù)據(jù)表,添加數(shù)據(jù)記錄等的一些sql語(yǔ)句了)
成都創(chuàng)新互聯(lián)是一家專注于成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作、外貿(mào)網(wǎng)站建設(shè)與策劃設(shè)計(jì),榆社網(wǎng)站建設(shè)哪家好?成都創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)10余年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:榆社等地區(qū)。榆社做網(wǎng)站價(jià)格咨詢:028-86922220
新建一個(gè)安裝文件:
1、連接數(shù)據(jù)庫(kù)(安裝的時(shí)候不是要填寫(xiě)一些數(shù)據(jù)庫(kù)連接參數(shù)等嗎)
2、讀取這個(gè)sql文件里的sql語(yǔ)句,并執(zhí)行
3、生成一個(gè)數(shù)據(jù)庫(kù)連接參數(shù)的php文件
就這么簡(jiǎn)單,思路是這樣啊,具體這么實(shí)現(xiàn),你自己慢慢研究
MYSQL本身沒(méi)有這種批量的命令。
不過(guò)你可以自己生成 alter table t1 modify 的腳本,一次性把所有需要修改的列的注釋更新。
你做好程序以后,把數(shù)據(jù)庫(kù)導(dǎo)出成sql文件
1、連接數(shù)據(jù)庫(kù)
2、讀取這個(gè)sql文件里的sql語(yǔ)句,并執(zhí)行
3、生成一個(gè)數(shù)據(jù)庫(kù)連接參數(shù)的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?{
//數(shù)據(jù)庫(kù)連接
protected?$connect?=?null;
//數(shù)據(jù)庫(kù)對(duì)象
protected?$db?=?null;
//sql文件
public?$sqlFile?=?"";
//sql語(yǔ)句集
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;
//連接數(shù)據(jù)庫(kù)
$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());
}
//導(dǎo)入sql文件
public?function?Import($url)?{
$this-sqlFile?=?file_get_contents($url);
if?(!$this-sqlFile)?{
exit("打開(kāi)文件錯(cuò)誤");
}?else?{
$this-GetSqlArr();
if?($this-Runsql())?{
return?true;
}
}
}
//獲取sql語(yǔ)句數(shù)組
public?function?GetSqlArr()?{
//去除注釋
$str?=?$this-sqlFile;
$str?=?preg_replace('/--.*/i',?'',?$str);
$str?=?preg_replace('/\/\*.*\*\/(\;)?/i',?'',?$str);
//去除空格?創(chuàng)建數(shù)組
$str?=?explode(";\n",?$str);
foreach?($str?as?$v)?{
$v?=?trim($v);
if?(empty($v))?{
continue;
}?else?{
$this-sqlArr[]?=?$v;
}
}
}
//執(zhí)行sql文件
public?function?RunSql()?{
foreach?($this-sqlArr?as?$k?=?$v)?{
if?(!mysql_query($v))?{
exit("sql語(yǔ)句錯(cuò)誤:第"?.?$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!";
}
?
mysql_select_db("table"
,conn)
or
die
("找不到數(shù)據(jù)源");
-----------------------------------------------------------------------
通過(guò)PHP創(chuàng)建MYSQL數(shù)據(jù)庫(kù)
conn
=
mysql_connect("localhost","root","password")
or
die("無(wú)法連接數(shù)據(jù)庫(kù)");
mysql_create_db("dwww")
or
die("無(wú)法創(chuàng)建數(shù)據(jù)庫(kù)");
sqlstr
=
"create
database
other_dwww";
----------------------------------------------------------------------------
創(chuàng)建mysql的表
conn
=
mysql_connect("localhost","root","password")
or
die("無(wú)法連接數(shù)據(jù)庫(kù)");
mysql_select_db("dwww",conn)
or
die("無(wú)法連接數(shù)據(jù)庫(kù)dwww");
sql
=
"create
table
dwww_table(
//判斷數(shù)組
colors
=
array(red,
blue,
green);
if(is_array(colors))
{
print(colors
is
an
array.br);
}
//雙精度數(shù)判斷
Temperature
=
15.23;
if(is_double(TemperatuPHP在線視頻教程
PHP視頻教程打包下載php自學(xué)視頻教程
PHP100視頻教程
PHP視頻教程,LAMP視頻教程PHP從入門(mén)到精通視頻教程
PHP+MYSQL網(wǎng)站設(shè)計(jì)入門(mén)實(shí)踐
PHP100視頻教程DVD光盤(pán)-第二季
PHP?
創(chuàng)建數(shù)據(jù)庫(kù):create database 數(shù)據(jù)庫(kù)名
創(chuàng)建數(shù)據(jù)表:
CREATE TABLE `users` (
`id` tinyint(10) auto_increment primary key NOT NULL,
`username` varchar(30) NOT NULL,
`age` int(10) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
執(zhí)行這兩個(gè)sql語(yǔ)句就行
if(!is_writable('config.php')){
emMsg('配置文件(config.php)不可寫(xiě)。如果您使用的是Unix/Linux主機(jī),請(qǐng)修改該文件的權(quán)限為777。如果您使用的是Windows主機(jī),請(qǐng)聯(lián)系管理員,將此文件設(shè)為可寫(xiě)');
}
if(!is_writable(EMLOG_ROOT.'/content/cache')){
emMsg('緩存文件不可寫(xiě)。如果您使用的是Unix/Linux主機(jī),請(qǐng)修改緩存目錄?(content/cache)?下所有文件的權(quán)限為777。如果您使用的是Windows主機(jī),請(qǐng)聯(lián)系管理員,將該目錄下所有文件設(shè)為可寫(xiě)');
}
$config?=?"?php\n"
."http://mysql?database?address\n"
."define('DB_HOST','$db_host');"
."\n//mysql?database?user\n"
."define('DB_USER','$db_user');"
."\n//database?password\n"
."define('DB_PASSWD','$db_pw');"
."\n//database?name\n"
."define('DB_NAME','$db_name');"
."\n//database?prefix\n"
."define('DB_PREFIX','$db_prefix');"
."\n//auth?key\n"
."define('AUTH_KEY','".getRandStr(32).md5($_SERVER['HTTP_USER_AGENT'])."');"
."\n//cookie?name\n"
."define('AUTH_COOKIE_NAME','EM_AUTHCOOKIE_".getRandStr(32,false)."');"
."\n";
$fp?=?@fopen('config.php',?'w');
$fw?=?@fwrite($fp,?$config);
if?(!$fw){
emMsg('配置文件(config.php)不可寫(xiě)。如果您使用的是Unix/Linux主機(jī),請(qǐng)修改該文件的權(quán)限為777。如果您使用的是Windows主機(jī),請(qǐng)聯(lián)系管理員,將此文件設(shè)為可寫(xiě)');
}
fclose($fp);
//密碼加密存儲(chǔ)
$PHPASS?=?new?PasswordHash(8,?true);
$adminpw?=?$PHPASS-HashPassword($adminpw);
$dbcharset?=?'utf8';
$type?=?'MYISAM';
$table_charset_sql?=?$DB-getMysqlVersion()??'4.1'???'ENGINE='.$type.'?DEFAULT?CHARSET='.$dbcharset.';'?:?'ENGINE='.$type.';';
if?($DB-getMysqlVersion()??'4.1'?){
$DB-query("ALTER?DATABASE?`{$db_name}`?DEFAULT?CHARACTER?SET?utf8?COLLATE?utf8_general_ci;",?true);
}
$widgets?=?Option::getWidgetTitle();
$sider_wg?=?Option::getDefWidget();
$widget_title?=?serialize($widgets);
$widgets?=?serialize($sider_wg);
define('BLOG_URL',?getBlogUrl());
$sql?=?"
DROP?TABLE?IF?EXISTS?{$db_prefix}blog;
CREATE?TABLE?{$db_prefix}blog?(
gid?int(10)?unsigned?NOT?NULL?auto_increment,
title?varchar(255)?NOT?NULL?default?'',
date?bigint(20)?NOT?NULL,
content?longtext?NOT?NULL,
excerpt?longtext?NOT?NULL,
alias?VARCHAR(200)?NOT?NULL?DEFAULT?'',
author?int(10)?NOT?NULL?default?'1',
sortid?int(10)?NOT?NULL?default?'-1',
type?varchar(20)?NOT?NULL?default?'blog',
views?int(10)?unsigned?NOT?NULL?default?'0',
comnum?int(10)?unsigned?NOT?NULL?default?'0',
attnum?int(10)?unsigned?NOT?NULL?default?'0',
top?enum('n','y')?NOT?NULL?default?'n',
sortop?enum('n','y')?NOT?NULL?default?'n',
hide?enum('n','y')?NOT?NULL?default?'n',
checked?enum('n','y')?NOT?NULL?default?'y',
allow_remark?enum('n','y')?NOT?NULL?default?'y',
password?varchar(255)?NOT?NULL?default?'',
template?varchar(255)?NOT?NULL?default?'',
PRIMARY?KEY??(gid),
KEY?date?(date),
KEY?author?(author),
KEY?sortid?(sortid),
KEY?type?(type),
KEY?views?(views),
KEY?comnum?(comnum),
KEY?hide?(hide)
)".$table_charset_sql."
INSERT?INTO?{$db_prefix}blog?(gid,title,date,content,excerpt,author,views,comnum,attnum,top,sortop,hide,allow_remark,password)?VALUES?(1,?'歡迎使用emlog',?'".time()."',?'恭喜您成功安裝了emlog,這是系統(tǒng)自動(dòng)生成的演示文章。編輯或者刪除它,然后開(kāi)始您的創(chuàng)作吧!',?'',?1,?0,?0,?0,?'n',?'n',?'n',?'y',?'');
DROP?TABLE?IF?EXISTS?{$db_prefix}attachment;
CREATE?TABLE?{$db_prefix}attachment?(
aid?int(10)?unsigned?NOT?NULL?auto_increment,
blogid?int(10)?unsigned?NOT?NULL?default?'0',
filename?varchar(255)?NOT?NULL?default?'',
filesize?int(10)?NOT?NULL?default?'0',
filepath?varchar(255)?NOT?NULL?default?'',
addtime?bigint(20)?NOT?NULL?default?'0',
width?int(10)?NOT?NULL?default?'0',
height?int(10)?NOT?NULL?default?'0',
mimetype?varchar(40)?NOT?NULL?default?'',
thumfor?int(10)?NOT?NULL?default?0,
PRIMARY?KEY??(aid),
KEY?blogid?(blogid)
)".$table_charset_sql."
DROP?TABLE?IF?EXISTS?{$db_prefix}comment;
CREATE?TABLE?{$db_prefix}comment?(
cid?int(10)?unsigned?NOT?NULL?auto_increment,
gid?int(10)?unsigned?NOT?NULL?default?'0',
pid?int(10)?unsigned?NOT?NULL?default?'0',
date?bigint(20)?NOT?NULL,
poster?varchar(20)?NOT?NULL?default?'',
comment?text?NOT?NULL,
mail?varchar(60)?NOT?NULL?default?'',
url?varchar(75)?NOT?NULL?default?'',
ip?varchar(128)?NOT?NULL?default?'',
hide?enum('n','y')?NOT?NULL?default?'n',
PRIMARY?KEY??(cid),
KEY?gid?(gid),
KEY?date?(date),
KEY?hide?(hide)
)".$table_charset_sql."
DROP?TABLE?IF?EXISTS?{$db_prefix}options;
CREATE?TABLE?{$db_prefix}options?(
option_id?INT(?11?)?UNSIGNED?NOT?NULL?auto_increment,
option_name?VARCHAR(?255?)?NOT?NULL?,
option_value?LONGTEXT?NOT?NULL?,
PRIMARY?KEY?(option_id),
KEY?option_name?(option_name)
)".$table_charset_sql."
INSERT?INTO?{$db_prefix}options?(option_name,?option_value)?VALUES?('blogname','點(diǎn)滴記憶');
INSERT?INTO?{$db_prefix}options?(option_name,?option_value)?VALUES?('bloginfo','使用emlog搭建的站點(diǎn)');
INSERT?INTO?{$db_prefix}options?(option_name,?option_value)?VALUES?('site_title','');
INSERT?INTO?{$db_prefix}options?(option_name,?option_value)?VALUES?('site_description','');
INSERT?INTO?{$db_prefix}options?(option_name,?option_value)?VALUES?('site_key','emlog');
INSERT?INTO?{$db_prefix}options?(option_name,?option_value)?VALUES?('log_title_style','0');
INSERT?INTO?{$db_prefix}options?(option_name,?option_value)?VALUES?('blogurl','".BLOG_URL."');
INSERT?INTO?{$db_prefix}options?(option_name,?option_value)?VALUES?('icp','');
INSERT?INTO?{$db_prefix}options?(option_name,?option_value)?VALUES?('footer_info','');
INSERT?INTO?{$db_prefix}options?(option_name,?option_value)?VALUES?('admin_perpage_num','15');
INSERT?INTO?{$db_prefix}options?(option_name,?option_value)?VALUES?('rss_output_num','0');
INSERT?INTO?{$db_prefix}options?(option_name,?option_value)?VALUES?('rss_output_fulltext','y');
INSERT?INTO?{$db_prefix}options?(option_name,?option_value)?VALUES?('index_lognum','10');
INSERT?INTO?{$db_prefix}options?(option_name,?option_value)?VALUES?('index_comnum','10');
INSERT?INTO?{$db_prefix}options?(option_name,?option_value)?VALUES?('index_twnum','10');
INSERT?INTO?{$db_prefix}options?(option_name,?option_value)?VALUES?('index_newtwnum','5');
INSERT?INTO?{$db_prefix}options?(option_name,?option_value)?VALUES?('index_newlognum','5');
INSERT?INTO?{$db_prefix}options?(option_name,?option_value)?VALUES?('index_randlognum','5');
INSERT?INTO?{$db_prefix}options?(option_name,?option_value)?VALUES?('index_hotlognum','5');
INSERT?INTO?{$db_prefix}options?(option_name,?option_value)?VALUES?('comment_subnum','20');
INSERT?INTO?{$db_prefix}options?(option_name,?option_value)?VALUES?('nonce_templet','default');
INSERT?INTO?{$db_prefix}options?(option_name,?option_value)?VALUES?('admin_style','default');
INSERT?INTO?{$db_prefix}options?(option_name,?option_value)?VALUES?('tpl_sidenum','1');
INSERT?INTO?{$db_prefix}options?(option_name,?option_value)?VALUES?('comment_code','n');
INSERT?INTO?{$db_prefix}options?(option_name,?option_value)?VALUES?('comment_needchinese','y');
INSERT?INTO?{$db_prefix}options?(option_name,?option_value)?VALUES?('comment_interval',60);
INSERT?INTO?{$db_prefix}options?(option_name,?option_value)?VALUES?('isgravatar','y');
INSERT?INTO?{$db_prefix}options?(option_name,?option_value)?VALUES?('isthumbnail','y');
INSERT?INTO?{$db_prefix}options?(option_name,?option_value)?VALUES?('att_maxsize','20480');
INSERT?INTO?{$db_prefix}options?(option_name,?option_value)?VALUES?('att_type','rar,zip,gif,jpg,jpeg,png,txt,pdf,docx,doc,xls,xlsx');
INSERT?INTO?{$db_prefix}options?(option_name,?option_value)?VALUES?('att_imgmaxw','420');
INSERT?INTO?{$db_prefix}options?(option_name,?option_value)?VALUES?('att_imgmaxh','460');
INSERT?INTO?{$db_prefix}options?(option_name,?option_value)?VALUES?('comment_paging','y');
INSERT?INTO?{$db_prefix}options?(option_name,?option_value)?VALUES?('comment_pnum','10');
INSERT?INTO?{$db_prefix}options?(option_name,?option_value)?VALUES?('comment_order','newer');
INSERT?INTO?{$db_prefix}options?(option_name,?option_value)?VALUES?('login_code','n');
INSERT?INTO?{$db_prefix}options?(option_name,?option_value)?VALUES?('reply_code','n');
INSERT?INTO?{$db_prefix}options?(option_name,?option_value)?VALUES?('iscomment','y');
INSERT?INTO?{$db_prefix}options?(option_name,?option_value)?VALUES?('ischkcomment','y');
INSERT?INTO?{$db_prefix}options?(option_name,?option_value)?VALUES?('ischkreply','n');
INSERT?INTO?{$db_prefix}options?(option_name,?option_value)?VALUES?('isurlrewrite','0');
INSERT?INTO?{$db_prefix}options?(option_name,?option_value)?VALUES?('isalias','n');
INSERT?INTO?{$db_prefix}options?(option_name,?option_value)?VALUES?('isalias_html','n');
INSERT?INTO?{$db_prefix}options?(option_name,?option_value)?VALUES?('isgzipenable','n');
INSERT?INTO?{$db_prefix}options?(option_name,?option_value)?VALUES?('isxmlrpcenable','n');
INSERT?INTO?{$db_prefix}options?(option_name,?option_value)?VALUES?('ismobile','n');
INSERT?INTO?{$db_prefix}options?(option_name,?option_value)?VALUES?('isexcerpt','n');
INSERT?INTO?{$db_prefix}options?(option_name,?option_value)?VALUES?('excerpt_subnum','300');
INSERT?INTO?{$db_prefix}options?(option_name,?option_value)?VALUES?('istwitter','y');
INSERT?INTO?{$db_prefix}options?(option_name,?option_value)?VALUES?('istreply','n');
INSERT?INTO?{$db_prefix}options?(option_name,?option_value)?VALUES?('topimg','content/templates/default/images/top/default.jpg');
INSERT?INTO?{$db_prefix}options?(option_name,?option_value)?VALUES?('custom_topimgs','a:0:{}');
INSERT?INTO?{$db_prefix}options?(option_name,?option_value)?VALUES?('timezone','8');
INSERT?INTO?{$db_prefix}options?(option_name,?option_value)?VALUES?('active_plugins','');
INSERT?INTO?{$db_prefix}options?(option_name,?option_value)?VALUES?('widget_title','$widget_title');
INSERT?INTO?{$db_prefix}options?(option_name,?option_value)?VALUES?('custom_widget','a:0:{}');
INSERT?INTO?{$db_prefix}options?(option_name,?option_value)?VALUES?('widgets1','$widgets');
INSERT?INTO?{$db_prefix}options?(option_name,?option_value)?VALUES?('widgets2','');
INSERT?INTO?{$db_prefix}options?(option_name,?option_value)?VALUES?('widgets3','');
INSERT?INTO?{$db_prefix}options?(option_name,?option_value)?VALUES?('widgets4','');