php动态绑定变量的用法

yipeiwu_com6年前PHP代码库

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

private function bindVars($stmt,$params) {
  if ($params != null) {
    $types = ''; //initial sting with types
    foreach($params as $param) {
 //for each element, determine type and add
      if(is_int($param)) {
        $types .= 'i'; //integer
      } elseif (is_float($param)) {
        $types .= 'd'; //double
      } elseif (is_string($param)) {
        $types .= 's'; //string
      } else {
        $types .= 'b';
 //blob and unknown
      }
    }
    $bind_names[] = $types;
 //first param needed is the type string
 // eg: 'issss'
    for ($i=0; $i<count($params);$i++) {
 //go through incoming params and added em to array
      $bind_name = 'bind' . $i;
   //give them an arbitrary name
      $$bind_name = $params[$i];
   //add the parameter to the variable variable
      $bind_names[] = &$$bind_name;
   //now associate the variable as an element in an array
    }
    //call the function bind_param with dynamic params
    call_user_func_array(array($stmt,'bind_param'),$bind_names);
  }
  return $stmt; //return the bound statement

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

相关文章

PHP实现双链表删除与插入节点的方法示例

本文实例讲述了PHP实现双链表删除与插入节点的方法。分享给大家供大家参考,具体如下: 概述: 双向链表也叫双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直接后继和直接前驱...

PHP防注入安全代码

简述:/*************************    说明:    判断传递的变量中是否含有非法字符 &...

PHP实现在对象之外访问其私有属性private及保护属性protected的方法

本文实例讲述了PHP实现在对象之外访问其私有属性private及保护属性protected的方法。分享给大家供大家参考,具体如下: public 表示全局的访问权限,类内部外部子类都可以...

php print EOF实现方法

我写段php代码如下: 复制代码 代码如下:<? if(test case) print<<<EOT <....html code....> EOF;...

PHP安全配置

PHP安全配置  一、Web服务器安全  PHP其实不过是Web服务器的一个模块功能,所以首先要保证Web服务器的安全。当然Web服务器要安全又必须是先保证系...