I want to create a class that connects to an ORACLE database using some OCI methods.
But when I call the Parse() method it is null and my FetchArray() method returns nothing
Database PHP class:
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); }
Test page.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'; }
Currently the database connection is successful.
Get array method:
public function FetchArray($mode = null) { oci_fetch_array($this->stmt, $mode); }
P粉5712335202024-01-30 09:20:38
I think I found a problem. In your while condition, you may have missed a few parentheses to make it compare the result of getting the row data to the boolean 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'; }
This line has been updated:
while (($row = $database->FetchArray(OCI_BOTH)) != false) {
I think it's also safe to remove the type conversion from the get function
public function FetchArray($mode = null):