Call to a member function num_rows() on boolean in PHP - php

I am getting the below error on my code,
Call to a member function num_rows() on boolean in C:\xampp\htdocs\codeigniter\application\controllers\go.php on line 15
Code:
<?php if (!defined('BASEPATH')) {
exit('No direct script access
allowed');
}
class Go extends MY_Controller
{
function __construct()
{
parent::__construct();
$this->load->helper('string');
}
public function index()
{
if (!$this->uri->segment(1)) {
redirect(base_url());
} else {
$url_code = $this->uri->segment(1);
$this->load->model('Urls_model');
$query = $this->Urls_model->fetch_url($url_code);
if ($query->num_rows() == 1) {
foreach ($query->result() as $row) {
$url_address = $row->url_address;
}
redirect(prep_url($url_address));
} else {
$page_data = array(
'success_fail' => NULL,
'encoded_url' => FALSE,
);
$this->load->view('common/header');
$this->load->view('nav/top_nav');
$this->load->view('create/create', $page_data);
$this->load->view('common/footer');
}
}
}
}

#ahmed is correct that db code belongs in the model but the problem you're having is most likely from a db error.
Besides fixing the problem, the workaround is this.
if ($query->num_rows() == 1) {
should be changed to
if ($query && $query->num_rows() == 1) {
I would put an else in there to dump the $this-db->error() to the error_log so you can see what the problem is.

You should call the 'num_rows()' inside the Model, not the Controller, for this function is used often after the get() method:
//Model function:
$query = $this->db->get();
$data = Array();
if($query->num_rows()){
//Work with data:
foreach($query->result_array() AS $row){
$data[] = $row;
}
}
return $data;
I suppose if you want to use the num_rows inside the controller, you should return the query AS a result like this:
$query = $this->db->get();
return $query;

$data = [];
$this->db->where('', 0);
$query = $this->db->get('');
if ($query->num_rows()>0)
{
foreach ($query->result_array() as $row)
{
$data[] = $row;
}
}
$query->free_result();
return $data;
First of all check that values come from database. if values come from table then put greater than 0 . if your value exist then query run forword. i thank this is help for you

This is caused by buggy Active Records in CI that adds an extra WHERE clause causing ambiguous column names and similar.
Try to get the actual query with $this->db->last_query(); and it will be instantly clear why $query is not an object.

Related

how can we use where condition if data is stored as implode

Here my table looks like this
i want to get all the departments if location_id is 3 that means if location id 3 is used i need to get the department_names of cardiology and hair so that i had done my code like this
My controller looks like this
public function get_location_departments()
{
$d = $_POST['location'];
$data['departments'] = $this->Hospital_model->get_location_departments($d);
$this->load->view('frontend/ajax_get_departments',$data);
}
My model looks like this
public function get_location_departments($d)
{
$this->db->where_in('location_id',$d);
$query=$this->db->get('department')->result();
return $query;
}
Please help me to solve my problem
Hope this will help you :
You have to query first to get the department and get location_id into an array using explode
Your model get_location_departments method should be like this :
public function get_location_departments($d)
{
if (! empty($d))
{
$query = $this->db->get('department');
if ($query->num_rows() > 0 )
{
foreach ($query->result() as $location)
{
$location_ids = explode(',',$location->location_id);
if (in_array($d, $location_ids))
{
$data[] = $location;
}
}
}
return $data;
//print_r($data);
}
}
imho the correct answer would be
public function get_location_departments($d)
{
$query = $this->db
->where('FIND_IN_SET('.$this->db->escape($d).', location_id)', NULL, false)
->get('department')
->result();
return $query;
}
There is a function called FIND_IN_SET for that purpose. You can find it in the mysql Documentation under https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_find-in-set

Invalid argument supplied Codeigniter

My program is not working properly, i do not know what should i do :S
I got this error message:
Take a look at this:
Here is my code:
My controller file (Home):
<?php
class Home extends CI_Controller{
public function __construct(){
parent::__construct();
$this->load->model("Crudmodel");
}
public function index(){
# get all data in Study table
$selectStudys = $this->Crudmodel->selectStudys();
foreach ($selectStudys as $key => $study)
{
# get UserNames
$user = $this->Crudmodel->getName($study['user_id']);
#get Subject Names
$subject = $this->Crudmodel->getSubName($study['subject_id']);
#append both NEW VALUES to same array
if(!empty($user[0]['username'])){
$data[$key]['user_id'] = $user[0]['username'];
// your main problem can be this. may be it is not getting value from query this is why we have put validation on model function and error handler condition here
}else{
$data[$key]['user_id'] = ''; // or anything as your else condition you can use as error handler
}
if(!empty($subject[0]['name'])){
$data[$key]['subject_id'] = $subject[0]['name'];
// your main problem can be this. may be it is not getting value from query this is why we have put validation on model function and error handler condition here
}else{
$data[$key]["subject_id"] = "";
// or anything you can use as error handler
}
}
$data['records'] = $selectStudys;
$this->load->view('home', $data);
}
}
?>
Crudmodel:
class Crudmodel extends CI_Model{
public function __construct(){
parent::__construct();
$this->load->database();
}
function selectStudys()
{
$query= $this->db->query("SELECT * FROM cursadas");
if($query->num_rows()>0){
$result = $query->result_array();
}else{
$result = "";
// or anything you can use as error handler
return $result;
}
}
function getName($name)
{
$query= $this->db->query("SELECT username FROM usuarios WHERE id = $name ");
if($query->num_rows()>0){
$result = $query->result_array();
}else{
$result = "";
// or anything you can use as error handler
return $result;
}
}
Dont know what to do now :(
Hope you can help me :S
The problem is in the model. You only return something inside the else. Easy fix, move the return.
You should probably return an empty array if there are no rows. Then the foreach will still have something to work with - even if it is empty. foreach will choke if given something that cannot be used in a loop - a string for instance.
function selectStudys()
{
$query= $this->db->query("SELECT * FROM cursadas");
if($query->num_rows()>0){
$result = $query->result_array();
}else{
$result = array();
}
return $result;
}

codeigniter retrieve data from database normal print_r working fine but can't use foreach why

I am going to fetch specific gender from db like gender='male' code is working fine but can't iterate it in foreach why it is
model you can check
class get_all_gender extends CI_Model {
//put your code here
public function select_male() {
$this->db->select('*');
$this->db->from('bride_groom_register');
$this->db->where("gender='male'");
$query = $this->db->get();
if ($query->result() > 0)
{
return $query;
} else {
return FALSE;
}
}
}
and controller i m getting data from database
class Welcome extends CI_Controller {
public function index()
{
$this->load->model('get_all_gender');
$data['res']=$this->get_all_gender->select_male();
$this->load->view('list',$data);
}
}
simple view
foreach ($res as $row)
{
echo $row->name;
}
Try this
In Model
class get_all_gender extends CI_Model {
//put your code here
public function select_male()
{
$this->db->select('*');
$this->db->from('bride_groom_register');
$this->db->where("gender='male'");
$query = $this->db->get();
$result = $query->result_array();
if (!empty($result))
{
return $result;
}
else {
return FALSE;
}
}
}
In Controller
class Welcome extends CI_Controller {
public function index()
{
$this->load->model('get_all_gender');
$data['res'] = $this->get_all_gender->select_male();
$this->load->view('list',$data);
}
}
In View
if ($res != FALSE) {
foreach ($res as $value) {
echo $value;
}
}
else {
echo "Seleted Category is Empty";
}
You should return the $query's result() instead of $query itself. Your model should look like this:
class Get_all_gender extends CI_Model {//put your code here
public function select_male() {
$this->db->where("gender","male");
$query = $this->db->get("bride_groom_register");
if ($query->num_rows() > 0) {
return $query->result();
} else {
return FALSE;
}
}
}
You are saving the result in the controller:
$data['res'] = $this->get_all_gender->select_male();
So you can access it like this in the view:
if($res !== FALSE){
foreach ($res as $row)
{
echo $row->name;
}
}
You should check for the FALSE condition, otherwise the view will throw PHP errors when there is no rows returned from the model.
Try code below on model
Class Name first letter upper case
Filename: Get_all_gender.php
<?php
class Get_all_gender extends CI_Model {//put your code here
public function select_male() {
$this->db->select('*');
$this->db->from('bride_groom_register');
$this->db->where('gender', 'male');
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->result_array();
// Or
// return $query->row_array();
} else {
return FALSE;
}
}
}
On View
foreach ($res as $row)
{
echo $row['name'];
}
Class Name first letter upper case
class Get_all_gender extends CI_Model {//put your code here
public function select_male() {
$this->db->select('*');
$this->db->from('bride_groom_register');
$this->db->where("gender='male'");
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->result_array();
} else {
return FALSE;
}
}
}

how to debug this? Call to a member function result() on a non-object

Please Help.. im using CODEIGNITER
I want to print the value of my userlevel and email using echo.. to know if im getting the right data from the database.. then suddenly this error keep bugging me.
beginner in CODEIGNITER need some help here.. thanks a lot!
in my controller:
if ($this->is_validated($rules))
{
$where = array('email' => $this->input->post('txt_email'));
$data['query'] = $this->database_model->select_userlevel('userlevel', $where);
$user = $data['query'];
foreach($user->result() as $row)
{
echo $row->email;
echo $row->userlevel;
}
}
in my model:
public function select_userlevel($table, $where)
{
$this->db->where($where);
$query = $this->db->get($table);
if ($query->num_rows() > 0)
{
return true;
}
else
{
echo 'no value';
return false;
}
}
Instead of return true you need to return your query in model
Model
public function select_userlevel($table, $where)
{
$this->db->where($where);
$query = $this->db->get($table);
if ($query->num_rows() > 0)
{
return $query;// return query here
}
else
{
return false;
}
Controller
$user = $this->database_model->select_userlevel('userlevel', $where);
foreach($user->result() as $row)
{
// your code here
}
UPDATED FOR better solution and proper use of MVC.
Instead of return query you need to return data from your model
MODEL
public function select_userlevel($table, $where)
{
$this->db->where($where);
$query = $this->db->get($table);
if ($query->num_rows() > 0)
{
return $query->result(); // return your result
}
else
{
return false;
}
}
CONTROLLER
$user = $this->database_model->select_userlevel('userlevel', $where);
foreach ($user as $row) {
// your code here
}
$user is not an active record object, so it does not have a reference to the result method.
All of this logic belongs in the Model and not the Controller.
Controller
public function doSomething()
{
if( !$this->is_validated($rules) )
return;
$email = $this->input->post('txt_email');
$users = $this->database_model->select_userlevel($email);
// for debugging
var_dump( $users )
die();
}
Model
public function select_userlevel($email)
{
$query = $this->db->select()
->where('email', $email)
->get('userlevel');
return ( $query->num_rows() > 0) ? $query->result() : false;
}

Code Igniter, array printing in view

This is my model
<?php
class home_model extends CI_Model {
function login()
{
$q = $this->db->query('SELECT * FROM users');
$count = $q->num_rows();
if ($count > 0)
{
foreach ($q->result() as $row)
{
$data[]['username'] = $row->username;
$data[]['email'] = $row->email;
$data[]['sex'] = $row->sex;
$data[]['dob'] = $row->dob;
$data[]['mobile'] = $row->mobile;
}
return $data;
}
?>
This is my controller
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class home extends CI_Controller
{
public function index()
{
$this->load->model('home_model');
$data['records'] = $this->home_model->login();
$this->load->view('home', $data);
}
}
I am only able to print_r and that is not how i want it, how will i echo this? I don't know what format I should use in my echo,
echo($records[0]);
This format is what I tried but it returns the error
A PHP Error was encountered
Severity: Notice
Message: Array to string conversion
Filename: views/home.php
Line Number: 4 Array
How should I code my view?
first of all your login function is need to corrected as per codeigniter.
function login() {
$q = $this->db->query('SELECT * FROM users');
$count = $q->num_rows();
if ($count > 0) {
return $q->result_array();
}
it will provide an array as you required.
then to use in views, you need to use it following.
suppose you want to echo first user name
echo $records[0]['username'];
Here $records[0] is an array and hence you need to use as above mention and similarly for other field.
Use following process
Your Modal
function login()
{
$q = $this->db->query('SELECT * FROM users');
$count = $q->num_rows();
if ($count > 0)
{
return $q->result();
}
}
?>
No change for controller
On View
foreach ($records as $row)
{
$username = $row->username;
$email = $row->email;
$sex = $row->sex;
$dob = $row->dob;
$mobile = $row->mobile;
}
In your views
foreach($records as $users)
{
echo $users['username'];
}
Use the builtin function print_r() and provide the array is an argument

Categories