select from mysql db with 300 tables using a default prefix

人走茶凉 提交于 2020-01-04 02:50:46

问题


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.


回答1:


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.




回答2:


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.




回答3:


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



回答4:


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


来源:https://stackoverflow.com/questions/5340899/select-from-mysql-db-with-300-tables-using-a-default-prefix

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!