?php
創(chuàng)新互聯(lián)主要從事做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)、網(wǎng)頁設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)二道江,十載網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):18980820575
class MysqlManage{
/*創(chuàng)建數(shù)據(jù)庫,并且主鍵是aid
* table 要查詢的表名
*/
function createTable($table){
$sql="CREATE TABLE IF NOT EXISTS `$table` (`aid` INT NOT NULL primary key)ENGINE = InnoDB;";
M()-execute($sql);
$this-checkTable($table);
}
/*
* 檢測表是否存在,也可以獲取表中所有字段的信息
* table 要查詢的表名
* return 表里所有字段的信息
*/
function checkTable($table){
$sql="desc `$table`";
$info=M()-execute($sql);
return $info;
}
/*
* 檢測字段是否存在,也可以獲取字段信息(只能是一個字段)
* table 表名
* field 字段名
*/
function checkField($table,$field){
$sql='desc `$table` $field';
$info=M()-execute($sql);
return $info;
}
/*
* 添加字段
* table 表名
* info 字段信息數(shù)組 array
* return 字段信息 array
*/
function addField($table,$info){
$sql="alter table `$table` add column";
$sql.=$this-filterFieldInfo();
M()-execute($sql);
$this-checkField($table,$info['name']);
}
/*
* 修改字段
* 不能修改字段名稱,只能修改
*/
function editField($table,$info){
$sql="alter table `$table` modify ";
$sql.=$this-filterFieldInfo($info);
M()-execute($sql);
$this-checkField($table,$info['name']);
}
/*
* 字段信息數(shù)組處理,供添加更新字段時候使用
* info[name] 字段名稱
* info[type] 字段類型
* info[length] 字段長度
* info[isNull] 是否為空
* info['default'] 字段默認(rèn)值
* info['comment'] 字段備注
*/
private function filterFieldInfo($info){
if(!is_array($info))
return
$newInfo=array();
$newInfo['name']=$info['name'];
$newInfo['type']=$info['type'];
switch($info['type']){
case 'varchar':
case 'char':
$newInfo['length']=empty($info['length'])?100:$info['length'];
$newInfo['isNull']=$info['isNull']==1?'NULL':'NOT NULL';
$newInfo['default']=empty($info['default'])?'':'DEFAULT '.$info['default'];
$newInfo['comment']=empty($info['comment'])?'':'COMMENT '.$info['comment'];
break;
case 'int':
$newInfo['length']=empty($info['length'])?7:$info['length'];
$newInfo['isNull']=$info['isNull']==1?'NULL':'NOT NULL';
$newInfo['default']=empty($info['default'])?'':'DEFAULT '.$info['default'];
$newInfo['comment']=empty($info['comment'])?'':'COMMENT '.$info['comment'];
break;
case 'text':
$newInfo['length']='';
$newInfo['isNull']=$info['isNull']==1?'NULL':'NOT NULL';
$newInfo['default']='';
$newInfo['comment']=empty($info['comment'])?'':'COMMENT '.$info['comment'];
break;
}
$sql=$newInfo['name']." ".$newInfo['type'];
$sql.=(!empty($newInfo['length']))?($newInfo['length']) .' ':' ';
$sql.=$newInfo['isNull'].' ';
$sql.=$newInfo['default'];
$sql.=$newInfo['comment'];
return $sql;
}
/*
* 刪除字段
* 如果返回了字段信息則說明刪除失敗,返回false,則為刪除成功
*/
function dropField($table,$field){
$sql="alter table `$table` drop column $field";
M()-execute($sql);
$this-checkField($table,$filed);
}
/*
* 獲取指定表中指定字段的信息(多字段)
*/
function getFieldInfo($table,$field){
$info=array();
if(is_string($field)){
$this-checkField($table,$field);
}else{
foreach($field as $v){
$info[$v]=$this-checkField($table,$v);
}
}
return $info;
}
}
輸入
然后用你設(shè)置的用戶名和密碼登入 用戶名一般為root
新建數(shù)據(jù)庫選下字符集就好了 輸入想新建的數(shù)據(jù)庫的字段數(shù)。。。
function add($name, $type, $size, $defaultvalue = '', $options = '', $title = '', $note = '', $formtype = '', $inputtool = '', $inputlimit = '', $enablehtml = 1, $enablelist = 1, $enablesearch = 0)
{
if(!in_array($type, $this-fieldtypes) || $this-exists($name)) return FALSE;
$size = intval($size);
$fieldsize = $type == 'varchar' ? min($size, 255) : ($type == 'int' ? min($size, 10) : 0);
$fieldtype = strtoupper($type);
if($fieldsize) $fieldtype .= "( $fieldsize )";
$this-db-query("ALTER TABLE $this-table ADD $name $fieldtype NOT NULL");
$this-db-query("INSERT INTO ".TABLE_FIELD."(tablename,name,type,size,defaultvalue,options,title,note,formtype,inputtool,inputlimit,enablehtml,enablelist,enablesearch) VALUES('$this-table','$name','$type','$size','$defaultvalue','$options','$title','$note','$formtype','$inputtool','$inputlimit','$enablehtml','$enablelist','$enablesearch')");
$result = $this-db-affected_rows();
$this-cache();
return $result;
}
function edit($fieldid, $type, $size, $defaultvalue = '', $options = '', $title = '', $note = '', $formtype = '', $inputtool = '', $inputlimit = '', $enablehtml = 1, $enablelist = 1, $enablesearch = 0)
{
if(!in_array($type, $this-fieldtypes)) return FALSE;
$fieldid = intval($fieldid);
$field = $this-get_info($fieldid);
$name = $field['name'];
$size = intval($size);
$fieldsize = $type == 'varchar' ? min($size, 255) : ($type == 'int' ? min($size, 10) : 0);
$fieldtype = strtoupper($type);
if($fieldsize) $fieldtype .= "( $fieldsize )";
$this-db-query("ALTER TABLE `$this-table` CHANGE `$name` `$name` $fieldtype NOT NULL");
$this-db-query("UPDATE ".TABLE_FIELD." SET title='$title',note='$note',type='$type',size='$size',defaultvalue='$defaultvalue',options='$options',formtype='$formtype',inputtool='$inputtool',inputlimit='$inputlimit',enablehtml='$enablehtml',enablelist='$enablelist',enablesearch='$enablesearch' WHERE fieldid=$fieldid");
$result = $this-db-affected_rows();
$this-cache();
return $result;
}
function delete($fieldid)
{
$fieldid = intval($fieldid);
$r = $this-db-get_one("SELECT name FROM ".TABLE_FIELD." WHERE fieldid=$fieldid");
if(!$r) return FALSE;
$name = $r['name'];
$this-db-query("ALTER TABLE $this-table DROP $name");
$this-db-query("DELETE FROM ".TABLE_FIELD." WHERE fieldid=$fieldid");
$result = $this-db-affected_rows();
$this-cache();
return $result;
}
db數(shù)據(jù)庫類去下個phpcms里面的就是上面的是自定義字段的操作函數(shù)
mysql_connect('地址','用戶名','密碼');
mysql_select_db('數(shù)據(jù)庫名');
$sql = "ALTER TABLE `表名` ADD `列名` 數(shù)據(jù)類型";
mysql_query($sql);
網(wǎng)站題目:php新建數(shù)據(jù)庫字段 php創(chuàng)建mysql數(shù)據(jù)庫
文章鏈接:http://www.muchs.cn/article48/doociep.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動網(wǎng)站建設(shè)、微信公眾號、網(wǎng)站導(dǎo)航、域名注冊、定制網(wǎng)站、品牌網(wǎng)站設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)