php动态绑定变量的用法

yipeiwu_com5年前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程序设计有所帮助。

相关文章

Zend Studio for Eclipse的java.lang.NullPointerException错误的解决方法

当然这个东西很吃内存,配置差点的就不推荐使用了. 最近用的时候, 发现, 建立PHP工程后,再打开 Zend Studio for Eclipse就出现这个错误 An internal...

PHP获取和操作配置文件php.ini的几个函数介绍

1.ini_get()获取配置参数,ini_set()设置配置参数 复制代码 代码如下:<?phpecho ini_get('display_errors'); //1//动态修改...

php找出指定范围内回文数且平方根也是回文数的方法

本文实例讲述了php找出指定范围内回文数且平方根也是回文数的方法。分享给大家供大家参考。具体如下: 一、要求: 给出两个数值X和Y,统计在这个区间里的回文数,并且要求它们的平方根也是回文...

php数据库密码的找回的步骤

1.用系统管理员登陆系统。 2.停止MySQL的服务。 3.进入命令窗口,然后进入MySQL的安装目录,比如我的安装目录是c:mysql,进入C:mysqlbin 4.跳过权限检查启动M...

php判断数组中是否存在指定键(key)的方法

本文实例讲述了php判断数组中是否存在指定键(key)的方法。分享给大家供大家参考。具体分析如下: php中有两个函数用来判断数组中是否包含指定的键,分别是array_key_exist...