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

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

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里面的就是上面的是自定义字段的 *** 作函数

亲,你的想法有问题,这是不符合要求的。

数据库中有很多的表,表中有字段。因此你不可能查询数据库中的id和news,而是只能在特定的表上查询。同时sql语法也要求

select

fields

from

table_name而不是db_name哦。

举例来说,保存新闻的表名字是news,

位于数据库

my_test_db中,那么应该

$con = mysql_connect("127.0.0.1", "123", "123")

mysql_select_db('my_test_db', $con)

$sql = "select id, news from news"后面的代码就一样了

$con = mysql_connect('localhost','root','')

if($con){

    mysql_query('use yatibang')

    mysql_query('set names utf8')

}else{

    echo "数据连接失败"

}

$sql = "desc app_category" 

$res = mysql_query($sql) 

$row = $rows = array()

while( $row = mysql_fetch_assoc($res)){ 

   $rows[] = $row['Field']

var_dump($rows)

返回结果如下:


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/9896709.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-03
下一篇 2023-05-03

发表评论

登录后才能评论

评论列表(0条)

保存