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

相关文章

php的ZipArchive类用法实例

本文实例讲述了php的ZipArchive类用法,分享给大家供大家参考。具体如下: 通常来说,php5.2开始支持ZipArchive类,php4只能使用zip函数。其实在官方实现zip...

PHP正则替换函数preg_replace()报错:Notice Use of undefined constant的解决方法分析

本文实例讲述了PHP正则替换函数preg_replace()报错:Notice Use of undefined constant的解决方法。分享给大家供大家参考,具体如下: 环境错误级...

Laravel 5.4重新登录实现跳转到登录前页面的原理和方法

前言 本文主要给大家介绍的是关于Laravel5.4重新登录跳转到登录前页面的相关内容,分享出来供大家参考学习,下面话不多说,来一起看看详细的介绍: 一、应用场景: 用户登陆后存在过...

详解PHP 二维数组排序保持键名不变

详解PHP 二维数组排序保持键名不变

 对二维数组指定的键名排序,首先大家想到的是array_multisort函数,关于array_multisort的用法我之前也写了一篇废话不多言,我们看个实例: <...

php5.2时间相差8小时

在PHP5中,在php.ini里修改 date.timezone = "Asia/shanghai" 就行了 ...