新聞中心
php中插入MySQL數(shù)據(jù)庫的語句怎么寫
顯示數(shù)據(jù)庫或表:
因?yàn)榕驼嬲\,有更多的客戶和我們聚集在一起,為了共同目標(biāo),成都創(chuàng)新互聯(lián)在工作上密切配合,從創(chuàng)業(yè)型企業(yè)到如今不斷成長,要感謝客戶對我們的高要求,讓我們敢于面對挑戰(zhàn),才有今天的進(jìn)步與發(fā)展。從網(wǎng)站到小程序開發(fā),軟件開發(fā),重慶App定制開發(fā),十載企業(yè)網(wǎng)站建設(shè)服務(wù)經(jīng)驗(yàn),為企業(yè)提供網(wǎng)站設(shè)計(jì),網(wǎng)站改版維護(hù)一條龍服務(wù).為企業(yè)提供成都全網(wǎng)營銷,定制開發(fā),原創(chuàng)設(shè)計(jì),十載品質(zhì),值得您的信賴.
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()可以刪除的.
如何實(shí)現(xiàn)PHP自動(dòng)創(chuàng)建數(shù)據(jù)庫
你做好程序以后,把數(shù)據(jù)庫導(dǎo)出成sql文件
1、連接數(shù)據(jù)庫
2、讀取這個(gè)sql文件里的sql語句,并執(zhí)行
3、生成一個(gè)數(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());
}
//導(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語句數(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語句錯(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!";
}
?
php弄數(shù)據(jù)庫一般怎么處理CREATE語句的?
1:可以在自己在文本里面寫好相關(guān)的建表語句,然后用phpmyadmin之類的工具導(dǎo)入到你的數(shù)據(jù)庫,然后在通過程序連接數(shù)據(jù)庫,插入,修改,刪除,查詢。一般開發(fā)流程都是在程序開發(fā)之前就需要規(guī)劃數(shù)據(jù)表的結(jié)構(gòu)的。(個(gè)人和企業(yè)自身用這樣就可以了)
2:就像你說的那樣創(chuàng)建一個(gè)文件專門用來創(chuàng)建數(shù)據(jù)庫和表,也就是程序的安裝模塊,當(dāng)然也是需要在文本中寫相關(guān)的sql語句的,之后通過程序?qū)氲綌?shù)據(jù)庫去,創(chuàng)建好了之后可以把他刪除。(這種一般給別人開發(fā)的時(shí)候用,像那些開源的cms都是這樣的)
怎么使用php代碼建立mysql數(shù)據(jù)庫
$link = mysql_pconnect("localhost","root","");
$sql = 'CREATE DATABASE my_db';
if (mysql_query($sql, $link)) {
echo "成功";
} else {
echo "失敗" . mysql_error() . "\n";
}
注:不提倡使用函數(shù)mysql_create_db()。最好用mysql_query()來提交一條SQLCREATEDATABASE語句來替代。
本文題目:php建數(shù)據(jù)庫語句 php數(shù)據(jù)庫怎么創(chuàng)建
鏈接分享:http://www.ef60e0e.cn/article/dddsjsc.html