php My Parsing方法返回空值(oci_parse()方法)

cig3rfwq  于 2022-12-10  发布在  PHP
关注(0)|答案(2)|浏览(113)

我想创建一个使用一些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';
        }

目前,数据库连接成功。
FetchArray方法:

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

oewdyzsn1#

现在,由于这个修改,我的Parse方法返回bool

public function Parse(string $sql): bool
    {
        $this->stmt = oci_parse($this->connection, $sql);
        if ($this->stmt) {
            return $this->stmt;
        } else {
            return false;
        }
    }
    public function Execute(): bool
    {
        if (oci_execute($this->stmt)) {
            return $this->stmt;
        } else {
            return false;
        }
    }

我的Execute方法返回true,但在调用FetchArray方法时出现此错误:正在尝试访问bool类型的值的数组偏移量

public function FetchArray($mode = null) : array | false
    {
        return oci_fetch_array($this->stmt, $mode);
    }
7lrncoxx

7lrncoxx2#

我想我发现了一个问题。在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) {

我认为从fetch函数中删除类型转换也是安全

public function FetchArray($mode = null):

相关问题