Home > Software engineering >  Call to undefined method CodeIgniter\Database\OCI8\Connection::_prepare()
Call to undefined method CodeIgniter\Database\OCI8\Connection::_prepare()

Time:12-31

I am using Codeigniter 4 with Oracle. Inside my controller, I have this code:

<?php

namespace App\Controllers;

class Home extends BaseController
{
    public function index()
    {

        $this->db = \Config\Database::connect();
        

        $query = "select * from test";

        $this->db->_prepare($query);
        $this->db->_execute();
        

        echo "<table border='1'>\n";
        $ncols =$this->db->getFieldCount();
        echo "<tr>\n";
        for ($i = 1; $i <= $ncols;   $i) {
            $colname = $this->db->getFieldNames($s, $i);
            echo "  <th><b>".htmlspecialchars($colname,ENT_QUOTES|ENT_SUBSTITUTE)."</b></th>\n";
        }
        echo "</tr>\n";

        while (($row = $this->db->fetchAssoc()) != false) {
            echo "<tr>\n";
            foreach ($row as $item) {
                echo "<td>";
                echo $item!==null?htmlspecialchars($item, ENT_QUOTES|ENT_SUBSTITUTE):"&nbsp;";
                echo "</td>\n";
            }
            echo "</tr>\n";
        }
        echo "</table>\n";

    }
}

This code is returning an error: Call to undefined method CodeIgniter\Database\OCI8\Connection::_prepare()

the method CodeIgniter\Database\OCI8\Connection is returning the oci_connect result

How could I make this work?

CodePudding user response:

Error:

Call to undefined method CodeIgniter\Database\OCI8\Connection::_prepare()

Explanation:

The error above is pretty clear. You're trying to call the _prepare(...) method which doesn't exist in the CodeIgniter\Database\OCI8\Connection class.

Solution:

<?php

namespace App\Controllers;

use CodeIgniter\Database\Query;

class Home extends BaseController
{
    public function index()
    {
        $db = db_connect();

        // Prepare the Query
        $preparedQuery = $db->prepare(static function ($db) {
            return (new Query($db))->setQuery('SELECT * FROM test WHERE 1=?');
        });

        $resultInterface = $preparedQuery->execute(1);
        $preparedQuery->close();

        $fieldNames = $resultInterface->getFieldNames();
        $fieldCount = $resultInterface->getFieldCount();
        $resultSet = $resultInterface->getResultArray();

        echo "<table border='1'>\n";

        echo "<tr>\n";
        foreach ($fieldNames as $fieldName) {
            echo "  <th><b>"
                . htmlspecialchars(
                    $fieldName,
                    ENT_QUOTES | ENT_SUBSTITUTE
                )
                . "</b></th>\n";

        }
        echo "</tr>\n";

        foreach ($resultSet as $row) {
            echo "<tr>\n";

            foreach (range(0, $fieldCount - 1) as $fieldIndex) {
                echo "<td>";
                echo !empty($row[$fieldNames[$fieldIndex]])
                    ? htmlspecialchars(
                        $row[$fieldNames[$fieldIndex]],
                        ENT_QUOTES | ENT_SUBSTITUTE
                    )
                    : "&nbsp;";
                echo "</td>\n";

            }
            echo "</tr>\n";
        }

         echo "</table>\n";

    }
}

Preparing the Query

This can be easily done with the prepare() method. This takes a single parameter, which is a Closure that returns a query object. Query objects are automatically generated by any of the “final” type queries, including insert, update, delete, replace, and get. This is handled the easiest by using the Query Builder to run a query. The query is not actually run, and the values don’t matter since they’re never applied, acting instead as placeholders. This returns a PreparedQuery object:

If you don’t want to use the Query Builder you can create the Query object manually using question marks for value placeholders:

Executing the Query

Once you have a prepared query you can use the execute() method to actually run the query. You can pass in as many variables as you need in the query parameters. The number of parameters you pass must match the number of placeholders in the query. They must also be passed in the same order as the placeholders appear in the original query:

getFieldCount()

The number of FIELDS (columns) returned by the query. Make sure to call the method using your query result object:

getFieldNames()

Returns an array with the names of the FIELDS (columns) returned by the query. Make sure to call the method using your query result object:

  • Related