mysql默认表前缀,使用默认前缀从具有300个表的mysql数据库中选择

I have a program that selects from about 200 tables with prefix. eg PBN_products, PBN_address, PBN_others.

Instead of appending the prefix on each table for the select statement, is there a way of defining the prefix as default value and do the selection?

$prefix=GET['prefix'];

mysql_connect(DB_SERVER, DB_SERVER_USERNAME, DB_SERVER_PASSWORD);

mysql_select_db(DB_DATABASE);

$sql = 'SELECT price, description, title, cost'.

'FROM products, address, others';

How can I define the prefix not to include in all tables? I have 200 tables.

解决方案

I would look into a class to do some simple query abstraction or some kind of ORM lib that does this. A sample would be like this.

class Query {

function from($tbl){

return new Table($tbl);

}

}

class Table {

var $prefix = 'PBN_';

var $tblname = '';

function Table($name){

$this->tblname = $this->prefix.$name;

}

function select($cols, $where = false, $order = false, $limit = false){

$query = "SELECT {$cols} FROM {$this->tblname}";

if($where) $query .= " WHERE ".$where; //add where

if($order) $query .= " ORDER BY ".$order; //add order

if($limit) $query .= " LIMIT ".$limit; //add limit

return $query;

}

}

$q = new Query;

$results = mysql_query($q->from('products')->select('*'));

This is obviously nowhere near complete or secure. Just a sample of how an abstraction class could speed up your sql and do you your prefixes for you.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值