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

相关文章

解决form中action属性后面?传递参数 获取不到的问题

如下所示: $p_id = $_REQUEST['p_id']; echo "<h1>您将更新商品编号为<span>$p_id</span>的商...

PHPExcel冻结(锁定)表头的简单实现方法

PHPExcel冻结(锁定)表头的简单实现方法

本文实例讲述了PHPExcel冻结(锁定)表头的简单实现方法。分享给大家供大家参考,具体如下: PHPExcel是一款功能比较强大的操作微软excel的PHP插件,在导出数据时为了方便查...

php操作zip在不解压缩包的情况下显示压缩包中的图片

PHP操作ZIP压缩包文件的基本方法大家应该都知道怎么做(如不了解可在本文底部学习PHP操作ZIP的基本方法),那么如何在不解压的情况下直接读取压缩包中的文件呢,如直接在页面中显示压缩包...

php之Memcache学习笔记

1、win下安装,memcached -d installwin下启动,memcached -d start关闭,memcached -d stop 1_1、三种方式访问memcache...

部署PHP项目应该注意的几点事项分享

在部署PHP项目时,有几点需要特别注意,也是初学者比较容易忽视的点: 一、下载WAMPServer后,如何安装配置? 二、如何通过客户端管理MySQL数据库? 三、如何通过IP地址访问P...