2014-05-04 42 views
0

我正在使用PHP和Oracle數據庫製作網站。 我有一個connect.php文件如何僅使用oci_connect()一次然後包含其文件

<?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_executeExecutes a statement previously returned from oci_parse().oci_parse需要2個參數之一是connection

回答

0

您需要在第一次調用的結果集oci_connect()給一個變量,像這樣:

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

然後,在您想要使用該連接的所有功能中,只需使用將其引用爲關鍵字:

function user_exists($username){ 
    global $conn; 

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