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源码的安装方法和实例

在官网下载源码包:https://www.php.net/downloads.php 步骤: 1、解压 命令:tar -xjvf php.tar.bz2 2、configure conf...

PHP文件系统基本操作类

<?php error_reporting(2047); /* * Class IO (SNakeVil 完成 03.25...

Eclipse的PHP插件PHPEclipse安装和使用

Eclipse的PHP插件PHPEclipse安装和使用

PHPEclipse是Eclipse的一个插件,提供了包括PHP语法分析、运行、调试等功能的集成开发环境。它基于Eclipse的插件机制,即插即用,配置和使用都非常方便。如果平时需要同时...

php生成二维码的几种方式整理及使用实例

php生成二维码的几种方式整理及使用实例

1.google开放api 复制代码 代码如下: $urlToEncode="http://bbs.lewanchina.com"; generateQRfromGoogle($u...

PHP实现读取文件夹及批量重命名文件操作示例

PHP实现读取文件夹及批量重命名文件操作示例

本文实例讲述了PHP实现读取文件夹及批量重命名文件操作。分享给大家供大家参考,具体如下: 以读取从网上下载的评书解压后的文件夹为例,并批量重命名 文件夹内容如下,现在使用php读取这个...