Codeigniter: Call to a member function result_array() on boolean in - php

I'm developing a website using Codeigniter framework. I wanna ask, I just got this message suddenly when I reload the page:
Call to a member function result_array() on boolean in
And even if I change into $this->db->result(), I still got the message:
Call to a member function result() on boolean in
and so when I call other sql query result function. Please help me, if anyone know the tricks and hints to solve this. Thanks.

It is really hard to answer without seeing the code. But hope this helps.
Try like this.
$query = $this->db->get('tablename');
$query->result();
or
$this->db->get('tablename')->result();

I php codeIgniter:-
result_array:-
This function returns the query result as a pure array, or an empty array when no result is produced. Typically you'll use this in a foreach loop, like this:
$query = $this->db->query("YOUR QUERY");
Message: Call to a member function result_array() on boolean
This error is the category of
Codeigniter Fatal error:
Fatal error : if the table is empty,2 Answers.
1-You should be checking to see if the query worked/has any rows before trying to get its results.
2-You should be checking to see if the query worked/has any rows before trying to get its results. If the table is empty, then the query won't do anything.
I have also face this issue when passing non-existing column name in my get_where() query().
In Code:-
<?php $teachers = $this->db->get_where('teachers',
array('is_deleted'=>0))->result_array();
foreach ($teachers as $row){
$count=$count+1;}
echo $count;
?>
Note:-
Like in code example make sure that the column name is_deleted which use provided to fetch data through get_where() also exit in table of your db.

Related

Retrieving rows from DB which contains same value in Laravel

Trying to retrieve rows which has same value(関西).
First I created a blade for this (kansai.blade)
And then I set the route:
Route::get('/kansai', 'PagesController#kansai');
I set the controller:
public function kansai()
{
$estates = allestates::where('region', '=', '関西')->get();
return view('pages.kansai', compact('estates'));
}
After that gave the link in main.blade:
<li>関西</li>
But it returns with an error:
Trying to get property of non-object (View:
/var/www/html/laravel/resources/views/welcome.blade.php)
Do I missing something here? The problem is my controller I guess?
Any idea? Thank you.
estates is an array not an object in this context. either loop trough it or specify an index.
I solved the problem, it's likely my mistake actually.
I already retrieved the data in kansai.blade.
So I just the pass the link in main.blade
which is like below.
<li>関西</li>
and with this problem solved.

Controller Variable not Showing up in Query Builder Laravel

I am encountering a strange issue in Laravel.
The below is an index function in one of my controllers.
public function index($merchant_url_text)
{
//
$deals = DB::table('tbl_deal')
-> join ('tbl_merchant', 'tbl_deal.merchant_id', '=', 'tbl_merchant.merchant_id')
-> where ('merchant_url_text', $merchant_url_text) -> toSql();
//return $merchant_url_text.$deal_id;
dd($deals);
//return $merchant_url_text;
}
As you can see I am passing merchant_url_text from route.
Route::get('/testroute/{merchant_url_text}', ['uses' =>'dealsVisibleController#index']);
When I am trying to debug the query by printing it, I am getting
"select * from `tbl_deal` inner join `tbl_merchant` on `tbl_deal`.`merchant_id` = `tbl_merchant`.`merchant_id` where `merchant_url_text` = ?"
This means that the query builder is not reading the $merchant_url_text variable. However, when I return just that variable, it is being printed.
Just can't figure out why the query builder is not able to include the $merchant_url_text variable in the query when it is available in the index function.
Any suggestions.
I am pretty sure that your code is correct. The SQL output function toSql() does not show the values of variables and only prints out a ? for security reasons.
You may access all your queries by using
$queries = DB::getQueryLog();
It is also printing the query parameters as array.
To get the last query:
dd(end($queries));
To disable the log:
DB::connection()->disableQueryLog();
See the docs for further information.

Laravel 5 Using orderBy() with Models

So I can't for the life of me figure out why I can't use orderBy. I'm getting the error:
Call to undefined method Illuminate\Database\Eloquent\Collection::orderBy()
Here is my code:
Route::get('/teams', function(){
$page = 'Teams';
$teams = App\Team::all()->orderBy('teamFirstName')->get();
return view('teams')->with('page', $page)->with('allTeams', $teams);
});
I have tried removing the ->get(); I have tried using just Team::all. I'm using a Laravel cheat sheet and I seem to be following the Model syntax. I have also double checked that it is the right column name in my DB and have even tried using just id.
If I remove the ->orderBy() the query works fine and I can get all of the teams. So what am I doing wrong?
It's because all() returns a Collection which does not have an orderBy method (though it does have a sortBy method which you could use). The following is a bit simpler though and should perform better.
$teams = App\Team::orderBy('teamFirstName')->get();
Once you call all(), the query is ran and results are fetched and any ordering done after this would be done by PHP. It's usually best to let the database handle the ordering though so use orderBy() first before fetching the results with get().
This line:
$teams = App\Team::all()->orderBy('teamFirstName')->get();
Need to be:
$teams = App\Team::orderBy('teamFirstName')->get();
Note:
user3158900 has a good explanation.

Laravel Eloquent ORM get function working Confusion

I am new to laravel so can anybody explain me that what exactly get() function of eloquent ORM does. I have following query and I need to know on which line the database is queried.
$propertiesQuery = Property::with('country', 'city','bannerInvoicesCount','rejectedBannerInvoicesCount')
->where('is_deleted','=',1);
if(Auth::user()->type == 'po') {
$propertiesQuery->where('user_id', '=', Auth::user()->id);
}
$propertiesQuery->orderBy('created_at', 'DESC');
$properties = $propertiesQuery->get();
Now in above code I have accessed Model once whether that is the point when db is queried or when I have called get() function. I am not been able to comprehend the internal working of this. That exactly on which statement the query is executed.
#Raza before eloquant $propertiesQuery->get() step, it prepare your query like:
select * from table where user_id=1
and when you call/trigger the $propertiesQuery->get() method, it forward your query to database and fetch the records against your query. like, normally we do in plain sql:
eg: mysql_query($query);
then mysql_fetch_assoc() etc methods
so get() method contains on: execute the query and return all the records in the array format.
I hope it will help you.

Codeigniter using different function's where clause

I have a Postgres database with a table for orders and a table order_contents, which includes all items of the order, and the order_id. I'm trying to make a view with order history.
In my model I have two functions, one to retrieve orders, and another to retrieve all the contents of an order, like so:
function retrieve_orders($userID){
$query = $this->db->get('orders');
$this->db->where('user_id', $userID);
return $query->result_array();
}
function get_order_contents($orderID){
$query = $this->db->get('order_contents');
$this->db->where('order_id', $orderID);
return $query->result_array();
}
So in my controller I call retrieve_orders() and pass the resulting array to the view. In the view, I want to create an HTML table for each order with all it's contents. So I iterate through each order and call get_order_contents in the loop.
Everything should work, however what happens is this error shows up in the source:
Error Number: ERROR: column "user_id" does not exist
LINE 3: WHERE "user_id" = '3'
^SELECT *
FROM "order_contents"
WHERE "user_id" = '3'
As you can see, for some reason, when I call get_order_contents() in the view, it uses the where clause I specified for the retrieve_orders() function.
What I have done so far is try to manually stop caching, but to no avail. Any help here is appreciated.
In each function, try to put where() before get().
When you try to get() order_contents it will use where clause from previous where().
Common practice is, use get() at the last call of $this->db after other calls such as where(), etc. See the Active Record documentation in codeigniter.
Chain your db functions like this
return $this->db->where('user_id', $userID)
->get('orders')
->result_array();
why not use $this->db->get_where()

Categories