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 array_map array_multisort 高效处理多维数组排序

对多维数组排序,通用的作法是1 获取利用排序的数据并且将其放入数组$arrSort. 其中键索引为要排序数组的索引,保证唯一性 2 利用排序函数sort等对$arrSort进行排序. 3...

php采集时被封ip的解决方法

在网上找了一些资料都没有找到,功夫不负有心人啊,在找的时侯有一个人提到了用搜索引擎爬虫蜘蛛的USERAGENT。虽然只提到一点点我还是想到了,列出我的解决方法, 1.使用Snoopy或c...

基于empty函数的输出详解

$a = '';echo '1.---------------'.empty($a).'<br>';$a = '0';echo '2.---------------'.emp...

PHP直接修改表内容DataGrid功能实现代码

PHP直接修改表内容DataGrid功能实现代码

由于需要连接Oracle所以从二次开发和页面样式来说个人觉得phpMyDataGrid还是比较好上手。 1. 创建测试数据库和表 create database `guru`;...

删除html标签得到纯文本可处理嵌套的标签

方法基本上来自THinkphp中的源码,但是被我修改了一下 复制代码 代码如下: <?php /* *@Description:删除HTML标签,得到纯文本。可以处理嵌套的标签 *...