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截取字符串并保留完整xml标签的函数代码

复制代码 代码如下:<?php      /**      * author: goosman &n...

PHP最常用的ini函数分析 针对PHP.ini配置文件

* ini_get():获取配置文件的选项值 这个函数相信很多人都使过,就是获取配置文件中某一个选项的值,如果是true值就返回1,如果是false值就返回0,字符串就返回字符串。 比...

php 获取远程网页内容的函数

<?php $curDomain = $_SERVER['HTTP_HOST']; $strHTML = file_get_contents('//www.jb51.net/Dom...

PHP+swoole+linux实现系统监控和性能优化操作示例

本文实例讲述了PHP+swoole+linux实现系统监控和性能优化操作。分享给大家供大家参考,具体如下: 服务器监控 端口监控php运行shell脚本 class Server {...

PHP解析xml格式数据工具类示例

本文实例讲述了PHP解析xml格式数据工具类。分享给大家供大家参考,具体如下: class ome_xml { /** * xml资源 * * @var res...