donglizhan7848 2011-03-17 15:04
浏览 98
已采纳

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

  • 写回答

4条回答 默认 最新

  • dtx9763 2011-03-17 15:24
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services