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 IE中下载附件问题解决方法

重点: 1、在IE中下载附件之前要清空缓存。 2、中文文件名要用urlencode编码。 复制代码 代码如下: Header("Pragma: "); //不加的话,IE中会提示目标主机...

php代码中使用换行及(\n或\r\n和br)的应用

代码a: 复制代码 代码如下: <?php echo'hello</br>'; echo'world!'; ?> output: helllo world! 代...

php简单生成随机颜色的方法

本文实例讲述了php简单生成随机颜色的方法。分享给大家供大家参考,具体如下: <?php //第一种方法: $rand = array('0', '1', '...

PHP使用curl请求实现post方式上传图片文件功能示例

本文实例讲述了PHP使用curl请求实现post方式上传图片文件功能。分享给大家供大家参考,具体如下: 在调用第三方api接口时,有时会遇到通过http协议上传图片,以下是一个微信公众平...

PHP转换IP地址到真实地址的方法详解

想要把IPv4地址转为真实的地址,肯定要参考IP数据库,商业的IP数据库存储在关系型数据库中,查询和使用都非常方便,但是成本不是个人和小公 司愿意承受的,所以简单应用的思路就是利用一些免...