【问题标题】:How to use oci_connect() only once and then include its file如何只使用一次 oci_connect() 然后包含它的文件
【发布时间】:2014-05-04 00:10:04
【问题描述】:

我正在使用 PHP 和 oracle 数据库制作网站。 我有一个 connect.php 文件

<?php 
$connect_error = "We are Experiencing Some Technical Difficulty";
oci_connect("asim","asim","localhost/xe") or die($connect_error);
?>

我在每一页都包含了这个文件!

但每当我必须执行查询时,我必须这样做

function user_exists($username){
    $conn = oci_connect("asim","asim","localhost/xe");

    $username = sanitize($username); 
    $stmt = oci_parse($conn,"SELECT COUNT(username)....");
    oci_execute($stmt); 
    return ($stmt > 0) ? true:false; 
}

我必须在每个函数中包含 $conn = oci_connect("asim","asim","localhost/xe"); 行。

有没有办法避免这种情况。

oci_execute Executes a statement previously returned from oci_parse(). oci_parse 需要 2 个参数,其中一个是 connection

【问题讨论】:

    标签: php oracle database-connection connection-string oracle-call-interface


    【解决方案1】:

    您需要将第一次调用 oci_connect() 的结果设置为如下变量:

    <?php 
    $connect_error = "We are Experiencing Some Technical Difficulty";
    $conn = oci_connect("asim","asim","localhost/xe") or die($connect_error);
    ?>
    

    然后在您想要使用该连接的所有函数中,您可以使用 global 关键字将其简单地引用为 global variable

    function user_exists($username){
        global $conn;
    
        $username = sanitize($username); 
        $stmt = oci_parse($conn,"SELECT COUNT(username)....");
        oci_execute($stmt); 
        return ($stmt > 0) ? true:false; 
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-16
      • 2017-05-17
      • 1970-01-01
      相关资源
      最近更新 更多