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开启与关闭错误提示适用于没有修改php.ini的权限

windows系统开关php错误提示 如果不具备修改php.ini的权限,可以将如下代码加入php文件中: 代码如下 ini_set("display_errors", "On"); e...

关于Laravel Service Provider开发设置延迟加载时遇到的问题详解

前言 本文主要介绍了关于Laravel Service Provider设置延迟加载时遇到的一些问题,之所有这篇文章,是因实际项目需求,近日在开发 laravel-database-lo...

php验证码的制作思路和实现方法

php验证码的制作思路和实现方法

一、制作思路 由于注册的时候常常会用到注册码来防止机器恶意注册,这里我发表一个产生png图片验证码的基本图像,简单的思路分析: 1、产生一张png的图片 2、为图片设置背景色 3、设置...

php下关于Cannot use a scalar value as an array的解决办法

今天在测试php程序的时候,出现了一个错误提示:Cannot use a scalar value as an array,这个错误提示前几天也出过,当时好像稍微调了一下就好了,也没深究...

PHP实现分布式memcache设置web集群session同步的方法

PHP实现分布式memcache设置web集群session同步的方法

本文实例讲述了PHP实现分布式memcache设置web集群session同步的方法。 php的session默认是文件存储: session.save_handler = file...