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的加密方式及原理

复制代码 代码如下: <?php //变量注意区分数字 "0" 和 字符"O" $OOO000000=urldecode('%66%67%36%73%62%65%68%70%72%...

中国站长站 For Dede4.0 采集规则

转载请注明出自落伍im286.com,本贴地址:http://www.im286.com/viewthread.php?tid=1991813 只差两分就落伍了,特献出中国站长站&nbs...

lnmp安装多版本PHP共存的方法详解

lnmp安装多版本PHP共存的方法详解

通过lnmp安装了PHP7版本,但是发现与程序不兼容,需要降低到7.0以下的版本。 查找lnmp的install.sh文件,一般在/root/lnmp1.5/install.sh 下执行...

PHP初学者最感迷茫的问题小结

【1】页面之间无法传递变量 get,post,session在最新的php版本中自动全局变量是关闭的,所以要从上一页面取得提交过来得变量要使用$_GET['foo'],$_POST['f...

PHP 截取字符串专题集合

1、UTF-8、GB2312都支持的汉字截取函数 复制代码 代码如下: <?php /* Utf-8、gb2312都支持的汉字截取函数 cut_str(字符串, 截取长度, 开始长...