oci_parse()方法傳回null的解析方法
P粉043432210
P粉043432210 2024-01-29 13:46:11
0
1
438

我想建立一個使用一些 OCI 方法連接到 ORACLE 資料庫的類別。

但是當我呼叫 Parse() 方法時它為 null,並且我的 FetchArray() 方法什麼也不回傳

資料庫 PHP 類別:

class Database
{
    /**
     * @var string
     */
    private string $login;
    /**
     * @var string
     */
    private string $password;
    /**
     * @var string
     */
    private string $description;
    /**
     * @var
     */
    private $connection;
    /**
     * @var
     */
    private $stmt;

    public function __construct(string $login, string $password, string $description)
    {
        $this->login = $login;
        $this->password = $password;
        $this->description = $description;
    }


    public function Connection($character_set = null, $session_mode = null)
    {
        $this->connection = oci_connect($this->login, $this->password, $this->description, $character_set,    $session_mode);
    }

   
    public function Parse(string $sql)
    {
        $this->stmt = oci_parse($this->connection, $sql);
    }


    public function Execute()
    {
        oci_execute($this->stmt);
    }

測試頁.php:

$database = new Database($login, $pass, $descr);
        $database->Connection();
        if ($database->IsConnected()) {
            $database->Parse($sql);
            $database->Execute();
            while ($row = $database->FetchArray(OCI_BOTH) != false) {
                // Use the uppercase column names for the associative array indices
                echo $row[0] . " and " . $row['EMAIL']   . " are the same<br>\n";
                echo $row[1] . " and " . $row['NAME'] . " are the same<br>\n";
            }
            $database->Disconnection();
        } else {
            echo 'Error';
        }

目前資料庫連線成功。

取得陣列方法:

public function FetchArray($mode = null)
    {
        oci_fetch_array($this->stmt, $mode);
    }

P粉043432210
P粉043432210

全部回覆(1)
P粉571233520

我想我發現了一個問題。在您的 while 條件下,您可能錯過了幾個括號,以使其將獲取行資料的結果與布林值 false 進行比較。

$database = new Database($login, $pass, $descr);
        $database->Connection();
        if ($database->IsConnected()) {
            $database->Parse($sql);
            $database->Execute();
            while (($row = $database->FetchArray(OCI_BOTH)) != false) {
                // Use the uppercase column names for the associative array indices
                echo $row[0] . " and " . $row['EMAIL']   . " are the same<br>\n";
                echo $row[1] . " and " . $row['NAME'] . " are the same<br>\n";
            }
            $database->Disconnection();
        } else {
            echo 'Error';
        }

此行已更新:

while (($row = $database->FetchArray(OCI_BOTH)) != false) {

我認為從獲取函數中刪除類型轉換也是安全的

public function FetchArray($mode = null):
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板