php動(dòng)態(tài)創(chuàng)建數(shù)據(jù)庫(kù)表 php 動(dòng)態(tài)創(chuàng)建類

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

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

我們提供的服務(wù)有:網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作、微信公眾號(hào)開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、海州ssl等。為千余家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的海州網(wǎng)站制作公司

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("打開文件錯(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í)候不是要填寫一些數(shù)據(jù)庫(kù)連接參數(shù)等嗎)

2、讀取這個(gè)sql文件里的sql語(yǔ)句,并執(zhí)行

3、生成一個(gè)數(shù)據(jù)庫(kù)連接參數(shù)的php文件

就這么簡(jiǎn)單,思路是這樣啊,具體這么實(shí)現(xiàn),你自己慢慢研究

如何在php創(chuàng)建數(shù)據(jù)庫(kù)與數(shù)據(jù)表

創(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ǔ)句就行

怎樣用laravel動(dòng)態(tài)創(chuàng)建數(shù)據(jù)庫(kù)表

怎樣用laravel動(dòng)態(tài)創(chuàng)建數(shù)據(jù)庫(kù)表

aravel提供了3種操作數(shù)據(jù)庫(kù)方式:DB facade(原始方式)、查詢構(gòu)造器和Eloquent ORM。下面我將逐一講解。 數(shù)據(jù)庫(kù)的配置文件在config目錄下的database.PHP里。打開這個(gè)文件,找到MySQL的配置項(xiàng)。 這里有個(gè)env,它其實(shí)是調(diào)用了laravel根目錄下的.e

網(wǎng)頁(yè)名稱:php動(dòng)態(tài)創(chuàng)建數(shù)據(jù)庫(kù)表 php 動(dòng)態(tài)創(chuàng)建類
本文來源:http://muchs.cn/article14/dogiede.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、企業(yè)建站、微信小程序、建站公司、ChatGPT、服務(wù)器托管

廣告

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

成都網(wǎng)站建設(shè)