首頁  >  問答  >  主體

oci_parse()方法傳回null的解析方法

我想建立一個使用一些 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粉043432210P粉043432210238 天前338

全部回覆(1)我來回復

  • P粉571233520

    P粉5712335202024-01-30 09:20:38

    我想我發現了一個問題。在您的 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):

    回覆
    0
  • 取消回覆