PHP函数通用数据库查询

时间:2013-09-17 05:36:18

标签: php database oracle function

代码中有几个地方对数据库有6-8个查询,查询,各种(选择,插入,更新,程序调用) 不能将所有查询合并在一起,我快速编写的功能代码类似于什么是必要的,但功能本身并不好看。 然而,还需要能够将多个变量附加到请求中。

也许有人,有类似的东西或帮我纠正这个功能。

更新:问题已经解决。

function execSql($conn,$xsql,$sql_no,$return_val,
                      $binding_val_name,$binding_val,
                       $bind_additional1,$bind_additional2){

    $stmt = oci_parse($conn, $xsql);
    if($binding_val_name !='')
      if($bind_additional1)
        oci_bind_by_name($stmt, $binding_val_name,
                          $binding_val,$bind_additional1,$bind_additional2);
      else
        oci_bind_by_name($stmt, $binding_val_name, $binding_val);

    oci_execute($stmt, OCI_NO_AUTO_COMMIT);
    if($return_val!='')
      oci_fetch_all($stmt,$result);

    checkError($stmt,$sql_no.'. '.$xsql);
    oci_free_statement($stmt);

    if($return_val!=''&&$return_val!='1')
      return $result[$return_val][0];
    if($binding_val_name!='')
      return $binding_val;
  }

和电话的例子

$doc = execSql($conn, $sql,'2', '',':doc','',8, SQLT_INT);
$reg_key = execSql($conn, $select,'3', 'REG_KEY',':doc',$doc);

使用数组

    if($binding_val_name !='')
          if(is_array($binding_val))
            if($bind_additional1[0])
              for ($i=0; $i < count($bind_additional1) ; $i++) { 
                oci_bind_by_name($stmt, $binding_val_name[$i],
 $binding_val[$i],$bind_additional1[$i],$bind_additional2[$i]);.
              }
            else
              for ($i=0; $i < count($bind_additional1) ; $i++) { 
                oci_bind_by_name($stmt, $binding_val_name[$i], $binding_val[$i]);
              }
          else
            if($bind_additional1)
              oci_bind_by_name($stmt, $binding_val_name,
 $binding_val,$bind_additional1,$bind_additional2);
            else
              oci_bind_by_name($stmt, $binding_val_name, $binding_val);

1 个答案:

答案 0 :(得分:0)

最后,我写了一个函数,通常会执行我的所有请求。

function execSql($conn,$xsql,$sql_no,$return_val, $binding_val_name,$binding_val,$bind_additional1,$bind_additional2){
    $stmt = oci_parse($conn, $xsql);
    if($binding_val_name !=''){
      if(is_array($binding_val_name))
        foreach ($binding_val_name as $key => $value) {
          oci_bind_by_name($stmt, $key, $binding_val_name[$key]);
        }
      else if($bind_additional1)
        oci_bind_by_name($stmt, $binding_val_name, $binding_val,$bind_additional1,$bind_additional2);
      else
        oci_bind_by_name($stmt, $binding_val_name, $binding_val);
    }
    oci_execute($stmt, OCI_NO_AUTO_COMMIT);
    if($return_val!='')
      oci_fetch_all($stmt,$result);

    $xsql = $xsql.' !!! sql_no:'.$sql_no.' return_val:'.$return_val.' binding_val_name:'./*(is_array($binding_val_name))?print_r($binding_val_name):*/$binding_val_name.' binding_val:'.$binding_val.' result:'.$result[$return_val][0];
    //echo /*' sql:'.$xsql.*/'!!! sql_no:'.$sql_no.' return_val:'.$return_val.' binding_val_name:'.$binding_val_name.' binding_val:'.$binding_val.' result:'.$result[$return_val][0];
    checkError($stmt,$sql_no.'. '.$xsql);
    oci_free_statement($stmt);

    if($return_val!=''&&$return_val!='1'||$return_val=='array')
      return (count($result)>1)?$result:$result[$return_val][0];
    if($binding_val_name!='')
      return $binding_val;
  }