您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

关于pdo引用传参,字段是不是不能当成参数传入?

2025/6/9 20:33:22发布10次查看
$pdo= new pdo ( 'mysql:host=localhost;dbname=test;', 'root', '123456', array ( pdo::mysql_attr_init_command => set names 'utf8', pdo::attr_errmode => pdo::errmode_exception) );$stmt=$pdo->prepare('select ? from tianya_post where id=1');$stmt->bindvalue(1, 'title');$stmt->execute();print_r($stmt->fetch());
第一行记录的标题是:“我和嫂子的那些事儿”
但,列位看官,猜下结果是什么?
array
(
    [title] => title
    [0] => title
)
为什么会这样?
如果用
select title from tianya_post where id=?
$stmt->bindvalue(1, 1);
可以得到正确的标题的。
回复讨论(解决方案) 字段列表不是参数!
http://blog.csdn.net/fdipzone/article/details/22330345
需要注意的是以下几种情况,pdo并不能帮助你防范sql注入。
不能让占位符 ? 代替一组值,这样只会获取到这组数据的第一个值,如:
select * from table where userid in ( ? );  
如果要用in?查找,可以改用find_in_set()实现
$ids = '1,2,3,4,5,6';  
select * from table where find_in_set(userid, ?);
不能让占位符代替数据表名或列名,如:
select * from table order by ?;
不能让占位符 ? 代替任何其他sql语法,如:
select extract( ? from addtime) as mytime from table;
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product