使用默认前缀从具有 300 个表的 mysql 数据库中选择
我有一个程序,可以从大约 200 个带有前缀的表中进行选择。例如PBN_产品、PBN_地址、PBN_其他。 有没有一种方法可以将前缀定义为默认值并进行选择,而不是在 select 语句的每个表上附加前缀?
$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';
如何定义不包含在所有表中的前缀?我有200张桌子。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我会研究一个类来执行一些简单的查询抽象或某种执行此操作的 ORM 库。一个样本会是这样的。
这显然远非完整或安全。这只是抽象类如何加速您的 sql 并为您添加前缀的示例。
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.
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.
您可以使用表名称定义一个数组,然后循环访问该数组。将数组项附加到字符串时,请将硬编码的“PBN_”放在该名称前面。
然后,您可以将所有表名添加到数组中,前缀将自动添加。
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.
You can then add all the tablenames to the array, and the prefix will automatically be added.
像这样的事情怎么样?
How about something like this?
你可以做这样的事情吗?
编辑:我同意这种不好的做法的评论...另一种方法是将前缀存储在另一个表中并在 GET 中传递该表的 ID。这将使您不易受到 SQL 注入的攻击。
You could do something like this?
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.