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中实现中文字符进制转换原理分析

PHP中实现中文字符进制转换原理分析

一,中文字符转十进制原理分析 GBK编码中一个汉字由二个字符组成,获取汉字字符串的方法如下 复制代码 代码如下: $string = "不要迷恋哥"; $length = strlen(...

PHP SESSION机制的理解与实例

PHP SESSION的保存机制有两种方式,session.save_handler = files和session.save_handler = user,具体选用哪种方式保存,可以通...

与文件上传有关的php配置参数总结

搞个了图片上传,死活不好使,后来发现是php参数配置的问题。 下面总结下与文件上传有关的php参数,备忘之~ 所有这些参数都在php.ini中设置。1.file_uploads 设为On...

PHP合并数组+与array_merge的区别分析

主要区别是两个或者多个数组中如果出现相同键名,键名分为字符串或者数字,需要注意 1)键名为数字时,array_merge()不会覆盖掉原来的值,但+合并数组则会把最先出现的值作为最终结果...

php读取torrent种子文件内容的方法(测试可用)

本文实例讲述了php读取torrent种子文件内容的方法。分享给大家供大家参考,具体如下: <?php /** * Class xBEncoder * Author...