原来,表名和字段名不能在pdo中“参数化查询”
Please see the following: http://us3.php.net/manual/en/book.pdo.php#69304
Table and Column names cannot be replaced by parameters in PDO.
In that case you will simply want to filter and sanitize the data manually. One way to do this is to pass in shorthand parameters to the function that will execute the query dynamically and then use a switch() statement to create a white list of valid values to be used for the table name or column name. That way no user input ever goes directly into the query. So for example:
function buildQuery( $get_var )
{
switch($get_var)
{
case 1:
$tbl = 'users';
break;
}
$sql = "SELECT * FROM $tbl";
}
By leaving no default case or using a default case that returns an error message you ensure that only values that you want used get used.
-----------------------------
To understand why binding a table (or column) name doesn't work, you have to understand how the placeholders in prepared statements work: they are not simply substituted in as (suitably escaped) strings, and the resulting SQL executed. Instead, a DBMS asked to "prepare" a statement comes up with a complete query plan for how it would execute that query, including which tables and indexes it would use, which will be the same regardless of how you fill in the placeholders.
The plan for SELECT name FROM my_table WHERE id = :value will be the same whatever you substitute for :value, but the seemingly similar SELECT name FROM :table WHERE id = :valuecannot be planned, because the DBMS has no idea what table you're actually going to select from.
This is not something an abstraction library like PDO can or should work around, either, since it would defeat the 2 key purposes of prepared statements: 1) to allow the database to decide in advance how a query will be run, and use the same plan multiple times; and 2) to prevent security issues by separating the logic of the query from the variable input.
原来,表名和字段名不能在pdo中“参数化查询”的更多相关文章
- SQL Server 2008 R2——根据数据查找表名和字段名 根据脏数据定位表和字段
=================================版权声明================================= 版权声明:原创文章 谢绝转载 请通过右侧公告中的“联系邮 ...
- mybatis动态调用表名和字段名
以后慢慢启用个人博客:http://www.yuanrengu.com/index.php/mybatis1021.html 一直在使用Mybatis这个ORM框架,都是使用mybatis里的一些常用 ...
- SQL 查询所有表名、字段名、类型、长度、存储过程、视图
-- 获得存储过程创建语句 select o.xtype,o.name,cm.text from syscomments cm inner join sysobjects o on o.id=cm.i ...
- sqlserver查询所有表名、字段名、类型、长度和存储过程、视图的创建语句
-- 获得存储过程创建语句 select o.xtype,o.name,cm.text from syscomments cm inner join sysobjects o on o.id=cm.i ...
- Oracle 查询库中所有表名、字段名、字段名说明,查询表的数据条数、表名、中文表名、
查询所有表名:select t.table_name from user_tables t;查询所有字段名:select t.column_name from user_col_comments t; ...
- oracle的表名、字段名、constraint名的长度限制分别是多少?
文章出处:http://blog.csdn.net/haiross/article/details/38379615 Oracle:表名.字段名.constraint名的长度有限制 oracle 的命 ...
- ibatis动态的传入表名、字段名
ibatis动态的传入表名.字段名,主要传入表名和字段名的不一致. Java代码: Map<String,Object> params = new HashMap<String,Ob ...
- SQL查询表,表的所有字段名,SQL查询表,表的所有字段名
SQL查询表,表的所有字段名 2011-07-29 10:21:43| 分类: SQLServer | 标签:表 sql 字段 |举报 |字号 订阅 SQL查询表,表的所有字段名 SQ ...
- SQL添加表字段以及SQL查询表,表的所有字段名
通用式: alter table [表名] add [字段名] 字段属性 default 缺省值 default 是可选参数 增加字段: alter table [表名] add 字段名 smalli ...
随机推荐
- Python3简明教程(五)—— 流程控制之循环
有些时候我们需要多次执行相同的任务,我们使用一个计数器来检查代码需要执行的次数.这个技术被称为循环. while循环 while语句的语法如下: while condition: statement1 ...
- Graveyard LA3708
白书第一章例题4 思维. 先固定一点不动,假设最后一共N个点,那么编号为0,1,...N-1, 0不动,原来的n个点分别占据i/n*N的位置(记为pos),移动到pos四舍五入的位置即可. 证明一:有 ...
- python基础一 day2 字符串操作
s.capitalize() s.upper() s.lower() s.swapcase() s.title() s.center(20,"#") s.expand ...
- HTML习题附答案
第一章 1.HTML指的是( A ). A超文本标记语言(Hyper Text Markup Language) B家庭工具标记语言(Home Tool Markup Language) C超 ...
- jquery 拖动(Draggable) 约束运动,输出数组排序Array
<!doctype html><html lang="en"><head> <meta charset="utf-8" ...
- linux 作为web应用服务器内核参数/etc/sysctl.conf
# Kernel sysctl configuration file for Red Hat Linux## For binary values, 0 is disabled, 1 is enable ...
- 慕课网 微信小程序商城构建全栈应用 tp5【总结】
1.异常处理: [代码越抽象,复用性越高] [封装性越好,适应代码变化的能力越强] [] <?php/** * Created by PhpStorm. * User: 14155 * Date ...
- 图的最小生成树——Kruskal算法
Kruskal算法 图的最小生成树的算法之一,运用并查集思想来求出最小生成树. 基本思路就是把所有边从小到大排序,依次遍历这些边.如果这条边所连接的两个点在一个连通块里,遍历下一条边,如果不在,就把这 ...
- springcloud了解
学习springcloud 文章标题:[置顶] 史上最简单的 SpringCloud 教程 | 终章 学习地址:http://blog.csdn.net/forezp/article/details/ ...
- POJ-2689 Prime Distance,区间素数筛法
Prime Distance 只会埃氏筛法的弱鸡今天读了读挑战程序设计120页,明白了求小区间内素数的方 ...