數(shù)據(jù)庫(kù)生成php 數(shù)據(jù)庫(kù)生成隨機(jī)數(shù)

如何實(shí)現(xiàn)PHP自動(dòng)創(chuàng)建數(shù)據(jù)庫(kù)

你做好程序以后,把數(shù)據(jù)庫(kù)導(dǎo)出成sql文件

10余年專(zhuān)注成都網(wǎng)站制作,企業(yè)網(wǎng)站建設(shè),個(gè)人網(wǎng)站制作服務(wù),為大家分享網(wǎng)站制作知識(shí)、方案,網(wǎng)站設(shè)計(jì)流程、步驟,成功服務(wù)上千家企業(yè)。為您提供網(wǎng)站建設(shè),網(wǎng)站制作,網(wǎng)頁(yè)設(shè)計(jì)及定制高端網(wǎng)站建設(shè)服務(wù),專(zhuān)注于企業(yè)網(wǎng)站建設(shè),高端網(wǎng)頁(yè)制作,對(duì)成都發(fā)電機(jī)回收等多個(gè)領(lǐng)域,擁有豐富的網(wǎng)站營(yíng)銷(xiāo)經(jīng)驗(yàn)。

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!";

}

?

怎么實(shí)現(xiàn)php自動(dòng)創(chuàng)建數(shù)據(jù)庫(kù),像discuz安裝時(shí)那樣,自己建數(shù)據(jù)庫(kù)和表?

你做好程序以后,把數(shù)據(jù)庫(kù)導(dǎo)出成sql文件(這個(gè)文件里就已經(jīng)有了一下創(chuàng)建數(shù)據(jù)表,添加數(shù)據(jù)記錄等的一些sql語(yǔ)句了)

新建一個(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),你自己慢慢研究

php讀取數(shù)據(jù)庫(kù)生成文件

?php

$datas?=?array();

$datas[]=array("id"=1,"name"="張飛");

$datas[]=array("id"=2?,"name"='趙云');?

//以JSON形式把數(shù)據(jù)保存到數(shù)據(jù)庫(kù)

$json_datas?=?json_encode($datas);??

mysql_query("INSERT?INTO?table_name?(datas)?VALUES?($json_datas)");

//取出時(shí)轉(zhuǎn)換JSON格式

$new_datas?=?js_decode($new_datas);

//這時(shí)候轉(zhuǎn)換后可以直接使用了?

?

當(dāng)前標(biāo)題:數(shù)據(jù)庫(kù)生成php 數(shù)據(jù)庫(kù)生成隨機(jī)數(shù)
URL地址:http://bm7419.com/article24/dohhsce.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供手機(jī)網(wǎng)站建設(shè)、定制網(wǎng)站用戶(hù)體驗(yàn)、網(wǎng)站內(nèi)鏈、企業(yè)建站商城網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

綿陽(yáng)服務(wù)器托管