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

我想创建一个使用一些 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):
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板