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有序表查找之二分查找(折半查找)算法。分享给大家供大家参考,具体如下: 简介: 二分查找技术,又称为折半查找。它的前提是线性表中的记录必须是关键码有序(通常从小到达有...

php正则preg_replace_callback函数用法实例

本文实例讲述了php正则preg_replace_callback函数的用法。分享给大家供大家参考。具体实现方法如下: php正则表达式功能强大,本范例演示了preg_replace_c...

php实现读取和写入tab分割的文件

本文实例讲述了php实现读取和写入tab分割的文件。分享给大家供大家参考。具体分析如下: 这段php代码实现读取和写入tab分割的文件,包含两个独立的函数,一个读,一个写,例如cvs文件...

php绘制一个扇形的方法

本文实例讲述了php绘制一个扇形的方法。分享给大家供大家参考。具体如下: php绘制一个扇形。关于参数说明,除最后一个参数外,其它都与弧线的参数一样,请参考上一篇《php绘制一条弧线的方...

php的curl实现get和post的代码

curl 支持SSL证书、HTTP POST、HTTP PUT 、FTP 上传,kerberos、基于HTT格式的上传、代理、cookie、用户+口令证明、文件传送恢复、http代理通道...