脚本宝典收集整理的这篇文章主要介绍了动态准备陈述是否不好? (用php mysqli),脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
// array of WHERE condITions $param = array('customer_id'=>1,'qty'=>'2'); $stmt = $MysqLi->stmt_init(); $tyPEs = ''; $bindParam = array(); $where = ''; $count = 0; // build the dynamic sql and param bind conditions foreach($param as $key=>$val) { $types .= 'i'; $bindParam[] = '$p'.$count.'=$param["'.$key.'"]'; $where .= "$key = ? AND "; $count++; } // prepare the query -- SELECT * From t1 WHERE customer_id = ? AND qty = ? $sql = "SELECT * From t1 WHERE ".substr($where,strlen($where)-4); $stmt->prepare($sql); // asSEMble the bind_param command $command = '$stmt->bind_param($types,'.implode(',',$bindParam).');'; // evaluate the command -- $stmt->bind_param($types,$p0=$param["customer_id"],$p1=$param["qty"]); eval($command);
最后一个eval()语句是个坏主意吗?我试图通过在变量名$param后面封装值来避免代码注入.
有没有人有意见或建议?我需要注意哪些问题?
试试这个:
>迭代params数组以构建带有问号的SELECT字符串“SELECT * FROM t1 WHERE p1 =?AND p2 =?”
>调用prepare()就可以了
>使用call_user_func_array()调用bind_@R_887_1403@,传入动态params数组.
代码:
call_user_func_array(array($stmt,'bind_param'),array($types)+$param);
以上是脚本宝典为你收集整理的动态准备陈述是否不好? (用php mysqli)全部内容,希望文章能够帮你解决动态准备陈述是否不好? (用php mysqli)所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。