189 8069 5689

php新建数据库字段 php创建mysql数据库

thinkphp 怎么实现对mysql做到创建表,修改字段,添加字段,删除字段

?php

创新互联主要从事做网站、成都网站设计、网页设计、企业做网站、公司建网站等业务。立足成都服务二道江,十载网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18980820575

class MysqlManage{

/*创建数据库,并且主键是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 字段信息数组 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']);

}

/*

* 字段信息数组处理,供添加更新字段时候使用

* info[name] 字段名称

* info[type] 字段类型

* info[length] 字段长度

* info[isNull] 是否为空

* info['default'] 字段默认值

* 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;

}

}

phpnow下怎么新建数据库

输入

然后用你设置的用户名和密码登入 用户名一般为root

新建数据库选下字符集就好了 输入想新建的数据库的字段数。。。

如何用ASP或PHP自动生成数据库字段名

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数据库类去下个phpcms里面的就是上面的是自定义字段的操作函数

用PHP怎么给数据库的表中添加字段

mysql_connect('地址','用户名','密码');

mysql_select_db('数据库名');

$sql = "ALTER TABLE `表名` ADD `列名` 数据类型";

mysql_query($sql);


网页标题:php新建数据库字段 php创建mysql数据库
网页地址:http://jkwzsj.com/article/doociep.html

其他资讯