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 7.4 中数组延展操作符语法知识点

详解PHP 7.4 中数组延展操作符语法知识点

在数组表达式中对展开操作符(Spread Operator)支持的 RFC 投票是 绝大多数人赞同 将此功能添加到 PHP 7.4。 扩展运算符支持参数解包首先存在于 PHP 5.6...

php封装的数据库函数与用法示例【参考thinkPHP】

本文实例讲述了php封装的数据库函数与用法。分享给大家供大家参考,具体如下: 从Thinkphp里面抽离出来的数据库模块,感觉挺好用 common.php: <?PHP...

使用PHP获取网络文件的实现代码

复制代码 代码如下:<?php //设置我们将要使用的文件 $srcurl = "http://localhost/index.php"; $tempfilename = "tem...

php 判断是否是中文/英文/数字示例代码

复制代码 代码如下: $str='asb天水市12'; if (preg_match("/^[\x7f-\xff]+$/", $str)){ echo '全部是汉字'; }else {...

php表单请求获得数据求和示例

获得表单请求的值: 案例: request.php 复制代码 代码如下: <html> <head> <meta http-equiv="content-t...