php創(chuàng)建數(shù)據(jù)庫的語句 php創(chuàng)建數(shù)據(jù)庫的語句怎么寫

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

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

創(chuàng)新互聯(lián)建站專注于企業(yè)全網(wǎng)營銷推廣、網(wǎng)站重做改版、輝縣網(wǎng)站定制設計、自適應品牌網(wǎng)站建設、H5建站、商城網(wǎng)站開發(fā)、集團公司官網(wǎng)建設、外貿(mào)網(wǎng)站建設、高端網(wǎng)站制作、響應式網(wǎng)頁設計等建站業(yè)務,價格優(yōu)惠性價比高,為輝縣等各大城市提供網(wǎng)站開發(fā)制作服務。

1、連接數(shù)據(jù)庫

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

3、生成一個數(shù)據(jù)庫連接參數(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ù)庫連接

protected?$connect?=?null;

//數(shù)據(jù)庫對象

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;

//連接數(shù)據(jù)庫

$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語句數(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語句錯誤:第"?.?$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數(shù)據(jù)庫的語句怎么寫

 顯示數(shù)據(jù)庫或表:

showdatabases;//然后可以usedatabase_name;

showtables;

更改表名:

altertabletable_namerenamenew_t;

添加列:

altertabletable_nameaddcolumnc_ncolumnattributes;

刪除列:

altertabletable_namedropcolumnc_n;

創(chuàng)建索引:

altertablec_tableaddindex(c_n1,c_n2);

altertablec_tableadduniqueindex_name(c_n);

altertablec_tableaddprimarykey(sid);

刪除索引:

altertablec_tabledropindexc_n1;

更改列信息:

alter tablet_tablechangec_1c_1varchar(200);

altertablet_tablemodify1c_1varchar(200);

insert插入語句:

insertintotable_name(c_1,c_2)

values('x1',1);

update語句:

update table_namesetc_1=1wherec_2=3;

刪除數(shù)據(jù)庫或者表:

droptabletable_name;

dropdatabasedatabase_name;//使用mysql_drop_db()可以刪除的.

如何使用php Myadmin創(chuàng)建Mysql數(shù)據(jù)庫

phpMyadmin是一個被普遍應用的網(wǎng)絡數(shù)據(jù)庫管理系統(tǒng),使用起來較為簡單,可以自動創(chuàng)建,也可以運行SQL語句創(chuàng)建,下面分別演示兩種方式創(chuàng)建Mysql數(shù)據(jù)庫的步驟:

一、使用菜單自動創(chuàng)建數(shù)據(jù)庫

登陸phpMyAdmin

在php MyAdmin右邊窗口中,填寫數(shù)據(jù)庫名稱,點創(chuàng)建即可。 例如我們這里創(chuàng)建一個名字為:cncmstest 的數(shù)據(jù)庫

創(chuàng)建成功之后提示如下

二、運行SQL語句創(chuàng)建數(shù)據(jù)庫

登陸phpMyAdmin

在phpMyAdmin主界面中點擊“SQL”超鏈接。如下圖所示

在SQL輸入窗口輸入建表語句,點右下角執(zhí)行即可,創(chuàng)建db_xuesheng數(shù)據(jù)庫為例代碼如下

create?database?db_xuesheng;

執(zhí)行成功界面如下

備注:其實兩種創(chuàng)建數(shù)據(jù)庫方式本質(zhì)是一樣的,第一種是利用系統(tǒng)菜單自動組合建庫SQL并執(zhí)行,第二種是手動輸入建庫SQL,如果熟悉SQL操作的話,第二種更快捷。

php弄數(shù)據(jù)庫一般怎么處理CREATE語句的?

1:可以在自己在文本里面寫好相關的建表語句,然后用phpmyadmin之類的工具導入到你的數(shù)據(jù)庫,然后在通過程序連接數(shù)據(jù)庫,插入,修改,刪除,查詢。一般開發(fā)流程都是在程序開發(fā)之前就需要規(guī)劃數(shù)據(jù)表的結構的。(個人和企業(yè)自身用這樣就可以了)

2:就像你說的那樣創(chuàng)建一個文件專門用來創(chuàng)建數(shù)據(jù)庫和表,也就是程序的安裝模塊,當然也是需要在文本中寫相關的sql語句的,之后通過程序導入到數(shù)據(jù)庫去,創(chuàng)建好了之后可以把他刪除。(這種一般給別人開發(fā)的時候用,像那些開源的cms都是這樣的)

當前名稱:php創(chuàng)建數(shù)據(jù)庫的語句 php創(chuàng)建數(shù)據(jù)庫的語句怎么寫
網(wǎng)站地址:http://muchs.cn/article26/doscecg.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化動態(tài)網(wǎng)站、關鍵詞優(yōu)化響應式網(wǎng)站、企業(yè)建站、網(wǎng)站改版

廣告

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

成都網(wǎng)站建設公司