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 5.3.1 安装包 VC9 VC6不同版本的区别是什么

最近在PHP官网上看到又有新版的PHP下载了,于是上去找找 For Windows的版本,可是一看,一共给了四个版本,VC9 x86 Non Thread Safe、VC9 x86 Th...

PHPExcel读取EXCEL中的图片并保存到本地的方法

HPExcel是相当强大的 MS Office Excel 文档生成类库,当需要输出比较复杂格式数据的时候,PHPExcel 是个不错的选择。 经过认真研究API文档和查看官方文档,终于...

php简单实现sql防注入的方法

本文实例讲述了php简单实现sql防注入的方法。分享给大家供大家参考,具体如下: 这里没有太多的过滤,主要是针对php和mysql的组合。 一般性的防注入,只要使用php的 addsla...

URL Rewrite的设置方法

URL Rewrite需要服务器的支持!在启用此设置之前,请确保服务器上已作出了正确的设置,设置方法请参看下边的“Apache下的设置方法”和“IIS下的设置方法”!Apach...

PHP中查询SQL Server或Sybase时TEXT字段被截断的解决方法

Author: Wenlong Wu 一、针对MS SQL SERVER数据库    有两种解决方案,如下:    修改php.ini来实现: 打开php.ini,可看到mssql.te...