使用默认前缀从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.

You could do something like this?

$prefix = '';
if(isset($_GET['prefix'])){
    $prefix = mysql_real_escape_string(stripslashes($_GET['prefix']));
}

$sql = "SELECT price, description, title, cost 
           FROM {$prefix}products, {$prefix}address, {$prefix}others";

EDIT: I agree on the comments that this is bad practice... An alternative would be to store the prefixes in another table and pass an ID of that table in the GET. This would make you less vulnarable to SQL injections.

$prefix = "";
if(isset($_GET['prefixid'])){
    $prefixid = mysql_real_escape_string(stripslashes($_GET['prefixid']));
    $query = "SELECT prefix FROM prefixes WHERE prefixid = $prefixid";
    $result = mysql_query($query);
    $prefix = mysql_result($result, 0, 0);
}
$sql = "SELECT price, description, title, cost 
           FROM {$prefix}products, {$prefix}address, {$prefix}others";

You could define an array with the table names, and loop through that array. When you append the array item to the string, put "PBN_" hardcoded in front of that name.

$arr = array("products","address","others");
$sql = "SELECT price, description, title, cost FROM ";
foreach ($arr as $tablename) {
    $sql = $sql . "PBN_" . $tablename . ", ";
}
$sql = substr($sql, 0, -2); // Remove last comma

You can then add all the tablenames to the array, and the prefix will automatically be added.

How about something like this?

 $prefix = GET['prefix'];

// add prefix to table names
foreach (array("products", "address", "others") as &$table) 
{
    $table = $prefix.$table;
}

mysql_connect(DB_SERVER, DB_SERVER_USERNAME, DB_SERVER_PASSWORD);
mysql_select_db(DB_DATABASE);

$sql = 'SELECT price, description, title, cost'.
       'FROM '.$table[0].', '.$table[1].', '.$table[2];