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去除重复字的实现代码

方法一: 复制代码 代码如下: $text = '数组aabbccdd'; $text_filter = ''; $filter = array(); $len = mb_strlen(...

php session实现多级目录存放实现代码

当一个目录下有很多文件时,服务器的处理性能会变低,php默认的session仅仅存放在/tmp目录下,未进行分级,当有一定的访问量时,就存在性能问题了。 首先,修改 php.ini的 s...

完美解决令人抓狂的zend studio 7代码提示(content Assist)速度慢的问题

完美解决令人抓狂的zend studio 7代码提示(content Assist)速度慢的问题

最近用zend studio7.2 遇到个问题,就是打开内容很多的php页面(>500行)时,编辑保存速度奇慢。根据网络上google到的资料 ,更改了content Assist...

memcache一致性hash的php实现方法

本文实例讲述了memcache一致性hash的php实现方法。分享给大家供大家参考。具体如下: 最近在看一些分布式方面的文章,所以就用php实现一致性hash来练练手,以前一般用的是最原...

PHP与C#分别格式化文件大小的代码

PHP 版: 复制代码 代码如下: function format($size) { $sizetext = array(" B", " KB", " MB", " GB", " TB"...