当前位置:Gxlcms > PHP教程 > php动态绑定变量的用法

php动态绑定变量的用法

时间:2021-07-01 10:21:17 帮助过:6人阅读

本文实例讲述了php动态绑定变量的用法。分享给大家供大家参考。具体如下:

  1. private function bindVars($stmt,$params) {
  2. if ($params != null) {
  3. $types = ''; //initial sting with types
  4. foreach($params as $param) {
  5. //for each element, determine type and add
  6. if(is_int($param)) {
  7. $types .= 'i'; //integer
  8. } elseif (is_float($param)) {
  9. $types .= 'd'; //double
  10. } elseif (is_string($param)) {
  11. $types .= 's'; //string
  12. } else {
  13. $types .= 'b';
  14. //blob and unknown
  15. }
  16. }
  17. $bind_names[] = $types;
  18. //first param needed is the type string
  19. // eg: 'issss'
  20. for ($i=0; $i<count($params);$i++) {
  21. //go through incoming params and added em to array
  22. $bind_name = 'bind' . $i;
  23. //give them an arbitrary name
  24. $$bind_name = $params[$i];
  25. //add the parameter to the variable variable
  26. $bind_names[] = &$$bind_name;
  27. //now associate the variable as an element in an array
  28. }
  29. //call the function bind_param with dynamic params
  30. call_user_func_array(array($stmt,'bind_param'),$bind_names);
  31. }
  32. return $stmt; //return the bound statement

希望本文所述对大家的php程序设计有所帮助。

人气教程排行