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程序设计有所帮助。

相关文章

浅谈Coreseek、Sphinx-for-chinaese、Sphinx+Scws的区别

Sphinx是一个基于SQL的全文检索引擎;普遍使用于很多网站 Sphinx的特性如下: a)  高速的建立索引(在当代CPU上,峰值性能可达到10 MB/秒); b)&nbs...

golang 调用 php7详解及实例

执行php文件 func Test_exec(t *testing.T) { engine.Initialize() ctx := &engine.Context{...

PHP中加密解密函数与DES加密解密实例

本文实例讲述了PHP中加密解密函数与DES加密解密的应用,分享给大家供大家参考。具体如下: 例子,php加密解密的例子 加密函数: 复制代码 代码如下:/* *功能:对字符串进行加密处理...

php判断str字符串是否是xml格式数据的方法示例

本文实例讲述了php判断str字符串是否是xml格式数据的方法。分享给大家供大家参考,具体如下: <?php //自定义xml验证函数xml_parser() func...

PHP设计模式之工厂模式详解

在开发大型系统时,往往会出现这样一种情况: 我有一部分基础数据,是类classA是从数据库A读取出来的,其他很多的功能都是基于这个基础数据来操作的。现在呢,我想把数据从数据库A变成从另外...