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

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):" ";
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?

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
)
: " ";
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

How do i fetch session id from database to controller and compare codeigniter

It Return undefined index uname also try $row->name not working return false
public function profile() {
$this->load->view('header');
$uname = $this->session->userdata('uname');
$row = $this->brid_groom_fetch->get_program_specific_gender();
if ($row['uname']->uname == $uname) {
$session_id = $this->session->userdata('session_id');
var_dump($session_id);
} else {
echo 'fail';
}
}
First you need to assign session variables to an array.
Then your code
$id = $this->session->userdata('uname')
will be relevent.
To assign you can use
$this->session->set_userdata(
array(
'uname' => $row->name
)
);
Where $row->name is the value fetched from your model query.
The above sets the value in session variable 'uname'
Now you can use $id wherever you want
Also Check your autoload.php file under config dir
$autoload['libraries'] = array('session');
should be present there.
If not, then you can do the above or call
$this->load->library('session')
in your controller function.
See if that helps

Iterating through result set in Laravel controller

I am trying to simply iterate though a result set in laravel on the controller side. This is what I tried but I get the following error:
Cannot use object of type stdClass as array
Controller snippet:
$result = DB::select($query);
foreach($result as $r){
echo $r['email'];
}
I appreciate any help with this,
Thanks in advance!
You need to use it as an object:
$result = DB::select($query);
foreach($result as $r){
echo $r->email;
}
Or if for some reason you want to use it as array, you need to convert it first:
$result = DB::select($query)->toArray();
foreach($result as $r){
echo $r['email'];
}
After pulling data from database you can convert it to array but it is optional, it's depends on your necessity then you can simply use the foreach to loop through to each distinct object and get access their property
$data = DB:: table('categories')
->where('parent_id', $request->value)
->get()->toArray();
$output = '<option value="">Select '.ucfirst($request->dependent).'</option>';
foreach($data as $row){
$output.= '<option value="'.$row->id.'">'.$row->title.'</option>';
}
return response($output, 200);
The Laravel Collection class supports the standard methods for iterables: map, sort, filter etc. see: vendor/laravel/framework/src/Illuminate/Collections/Collection.php
if you're querying on a model:
$results = MyAwesomeModel::where('something' '=' 'other')->get();
//this is valid
$results->map(function($result) {
echo $result->field;
});
//this is also valid
for($i = 0; $i < sizeof($results); $i++){
echo $results[$i]->field;
}
I would suggest avoiding ->toArray() on hydrated results (instances of models) because you loose all the model properties.

How to concatenate variables comes from model in controller

I need to concatenate variables come from model.I send the role_id from controller to model and get the role name according to its id.
controller:
function get_role_name(){
$data['rec']=$this->amodel->get_section();
foreach($data['rec'] as $i)
{
$name['x']=$this->amodel->get_name($i->role_id);
}
$this->load->view('sections',array_merge($data,$name));
}
I write $name['x'].=$this->amodel->get_name($i->role_id); but it shows the error which undefined index:x.How can I concatenate the role name in cotroller to send it to view?
you probably didnt define $name
$name = array();
// your foreach
foreach ()
btw, the concatenating was ok
$var = "foo";
$var .= "foo"
// will result in "foofoo"
If you want to append something using the .= syntax you need to make sure the variable or array exists first.
Try this:
function get_role_name(){
$data['rec']=$this->amodel->get_section();
$name = array();
foreach($data['rec'] as $i) {
if (isset($name['x'])) {
$name['x'] .= $this->amodel->get_name($i->role_id);
} else {
$name['x'] = $this->amodel->get_name($i->role_id);
}
}
$this->load->view('sections',array_merge($data,$name));
}

Code Igniter, multiple passing parameters in method

I have an method in Code Igniter, and want access using this URL "kuesioner/test/school_id/6/parent_id/7"
I using _remap function to dynamic order of params in URL. Code from this.
This is my code :
public function test($school_id, $parent_id)
{
echo "<pre>";
print_r($schoold_id);
print_r($parent_id);
echo "</pre>";
}
public function _remap($method, $params)
{
$map = array();
for( $i = 1; $i < count( $params ); $i = $i + 2 )
{
$map[$params[$i-1]] = $params[$i];
}
if( $method[0] != '_' && method_exists( $this, $method ))
return $this->$method($map);
}
Getting error on my controller that said Missing argument 2 for Kuesioner::test() ($parent_id is missing), it return array of map into single variable $school_id in test controller.
print_r($school_id) exactly show
Array
(
[school_id] => 6
[parent_id] => 7
)
How to solve this, so that value can be put into every right variable..
Always assign parameter with default values like 0 or anything else it will prevent from unnecessary warning and errors.
public function test($school_id = 0, $parent_id = 0)
{
echo "<pre>";
print_r($schoold_id);
print_r($parent_id);
echo "</pre>";
}
public function test($params = NULL)
{
print_r($this->uri->uri_to_assoc(3));
// #CHECK http://ellislab.com/codeigniter/user-guide/libraries/uri.html
}
Try to replace last line with this:
return call_user_func_array(array($this, $method), $map);
I think that would solve your issue,however if not, you can edit your "test" function to :
public function test($params)
{
echo "<pre>";
print_r($params['schoold_id']);
print_r($params['parent_id']);
echo "</pre>";
}
why you don't try with $this->uri->uri_to_assoc(n).
See detail : http://ellislab.com/codeigniter/user-guide/libraries/uri.html
don't need to use _remap

Getting data from model to controller

I do have a question : I cannot pass the data from model to controller
You can see some of my codes, please help me.
It does not work!
this is my model "mymodel.php"
....
$query = $this->db->query("SELECT * FROM `rand` WHERE `used` = 0 LIMIT 0, 1");
if($query){
foreach ($query->result() as $row);
}
$t = "EXAMPLE/{$row->code}";
function wandad() {
return $t;
}
.....
and this is my controller mycont.php
...
$this->load->model('mymodel');
$data['new'] = $this->Mymodel->wandad();
$this->load->view('myview',$data);
...
and this is my view myview.php
....
echo $new;
.....
Clearly you The model is not written properly and to corrent this simple do this
1.) I put a default value on $t
2.) I put the query >> loop on the inside function
wandad
so it can be executed once called from controller
function wandad() {
$query = $this->db->query("SELECT * FROM `rand` WHERE `used` = 0 LIMIT 0, 1");
$t = "";
if($query){
foreach ($query->result() as $row){
$t = "EXAMPLE/{$row->code}".'<br>';
}
}
return $t;
}
Here are several issues into your model
Your Foreach function doesn't do anything
$t is not in the same namespace than wandad()
Function wandad is not defined into your model class
I'm not sure of what you wanna get with wandad() function but here's a pattern.
function yourFunction()
{
/* This will return the full query as an array */
$query = $this->db->query("SELECT * FROM `rand` WHERE `used` = 0 LIMIT 0, 1")->result_array();
/* Store variable in the same class */
$this->t = "EXAMPLE/".$query[0]['code'];
/* Close yourFunction() */
}
public function wandad() {
return $this->t;
}
Then into your controller, do that instead :
$this->load->model('mymodel');
$this->mymodel->yourFunction();
$data['new'] = $this->mymodel->wandad();
$this->load->view('myview',$data);
#Touki his foreach actually does something. It will set the variable $row with the last row that is returned from the query. Not the best way to do it ... But it's a way. Better would be to use a limit in the query.
There is a small mistake in your code #Ernesto.that is
foreach ($query->result() as $row){
$t. = "EXAMPLE/{$row->code}".'<br>';
}
but your code was simply nice

Resources