I want to create a class that uses some OCI methods to connect to ORACLE databases.
But my Parse() method is null when I call it 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 connection to the database is successful.
FetchArray method :
public function FetchArray($mode = null)
{
oci_fetch_array($this->stmt, $mode);
}
CodePudding user response:
Now, my Parse method return bool, thanks to this modification
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;
}
}
My Execute method return true but i have this error when i call FetchArray method : Trying to access array offset on value of type bool
public function FetchArray($mode = null) : array | false
{
return oci_fetch_array($this->stmt, $mode);
}
CodePudding user response:
I think I found a problem. In your while condition, you probaby missed couple brackets, to make it comparing result of fetch to row data to 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 is updated:
while (($row = $database->FetchArray(OCI_BOTH)) != false) {
I think it would be also safe to remove type casting from fetch function
public function FetchArray($mode = null):