Invalid argument supplied Codeigniter - php

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;
}

Related

Cannot use object of type QueryResult as array, Using Salesforce library with CI

I got an error
"Cannot use object of type QueryResult as array"
I am simply trying to take state name from the records array but, I only manage to get this error "Cannot use object of type QueryResult as array"
Model.php
public function getstatename()
{
$statename = $this->salesforce->query ("SELECT State__c FROM RI_School_List__c group by State__c");
return $statename;
}
Controller.php
public function index() {
$data['getstatename'] = $this->model->getstatename();
$this->load->view('footer', $data);
echo "<pre>";
print_r($data['getstatename']['records']);
}
Actually $statename is result-set object not an array.
So you need to do like below:-
Model.php:-
public function getstatename(){
$statename = $this->salesforce->query ("SELECT State__c FROM RI_School_List__c group by State__c");
$state_array = array(); // create an array
foreach ($statename as $row){
$state_array[] = $row->State__c; // assign value to array
}
return $state_array; // return array
}
Controller.php:-
public function index() {
$data['getstatename'] = $this->Footer_model->getstatename();
echo "<pre/>";print_r($data['getstatename']); // check this and accordingly change you code further
$this->load->view('footer', $data);
}
Here is the solved code Thanks to #Anant
Model.php
public function getstatename()
{
$statename = $this->salesforce->query ("SELECT State__c FROM RI_School_List__c group by State__c");
$state_array = array();
foreach ($statename as $row){
$state_array[] = $row->State__c;
}
return $state_array;
}
Controller.php
public function index() {
$data['getstatename'] = $this->Footer_model->getstatename();
$this->load->view('footer', $data);
}
See if you can do
$data['getstatename']->fetchRow();

Call to a member function num_rows() on boolean in 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.

Object of Class could not be converted to string CI Error

I am getting an error in the bellow code and I am unable to find out what is the reason for it. I am glad if you can help me with this.
Public function () {
$query = $this->db->select_sum("cost_after_discount");
$query = $this->db->get('tbl_payment');
$data = $query;
return $data;
}
Object of Class could not be converted to string CI Error
There are several errors in your code. First you have to provide a name for your function then you do not use any result method to return result. So try following
Public function xyz() {
$this->db->select_sum("cost_after_discount");
$query = $this->db->get('tbl_payment');
$data = $query->result_array();
return $data;
}
now you may call this xyz function to retrieve data
Try this
public function retrive()
{
$this->db->select_sum("cost_after_discount");
$query = $this->db->get('tbl_payment');
$data = $query->result_array();
var_dump($data);
}
This should do :)

how to store database query result in a variable in view

I want to store my database query result in my view section in a variable.I am trying but not work.
My View code
$cd=''.base_url().'/video/series/';
$count = count($cd);
for ($i = 0; $i < $count; $i++)
{
print'"'.$cd[$i][1].'",';
}
My Controller code
public function series() {
$result= $this->video_model->series_list();
return $result;
}
My Model Code
function series_list()
{
$string = trim($this->input->get_post('term'));
$query = $this->db->query("SELECT name FROM `series` WHERE name LIKE '%".$string."%'");
return $query->row_array();
}
$cd=''.base_url().'/video/series/'; not get any array data ,only get blank data
You can get post variable value in controller only. But you trying to get it in model. That's wrong.
Controller:
public function series() {
$string = trim($this->input->get_post('term'));
$data['result']= $this->video_model->series_list($string);
$this->load->view('folder/filename', $data);
// in your case i think folder= video and filename = series
// in this way you can pass value from controller to view
}
Model:
function series_list($string = null)
{
if($string != ''){
$query = $this->db->query("SELECT name FROM `series` WHERE name LIKE '%".$string."%'");
return $query->row_array();
}
else
return false;
}
View:
<?php
var_dump($data);
?>
you can get your resultset in view and can play with it as you want.
You need to pass variable inside some array(in view method) so that all variables inside common array would be accessible on view page
Controller code would be
public function series()
{
$inputData =trim(strip_tags($this->input->get_post('term')));
$data['result']= $this->video_model->series_list($inputData);
$this->load->view('directory/viewpage',$data); //$data is array with all variables inside it
}
Model code would be
function series_list($data)
{
$string = $data;
$query = $this->db->query("SELECT name FROM `series` WHERE name LIKE '%".$string."%'");
if( $query->num_rows>0)
{
return $query->row_array();
}
else
{
return false;
}
}
View code would be
<?php
if(isset($result)&&($result!=''))
{
echo "<pre/>";
print_r($result); // Or var_dump($result);
}
?>

Codeigniter: Call to a member function result_array() on a non-object

I'm using Codeigniter to build a webapp and I received this error:
Fatal error: Call to a member function result_array() on a
non-object in /var/www/application/controllers/people.php on line 29
This is the people.php class:
public function __construct()
{
parent::__construct();
}
function People() {
}
function view($id) {
echo "Hello world " . $id;
}
function all() {
$this->load->model('people_model', '', TRUE);
$allContacts = $this->people_model->get_all_contacts();
$results = array();
foreach ($allContacts->result_array() as $row) {
$eachrow = array("personID"=>$row['personID'],"fName"=>$row['fName'],"lName"=>$row['lName'],"phoneNumber"=>"",
"emailAddress"=>$row['emailAddress'],"address"=>$row['address'],"city"=>$row['city'],"state"=>$row['state'],"zip"=>$row['zip']);
$results = push_array($results, $eachrow);
}
foreach ($results as $row) {
$phoneData = $this->people_model->get_phone_by_id($row['personID']);
$phoneNumbers = "";
foreach ($phoneData->result_array() as $row2) {
$phoneNumbers = $row2['type'] . " " . $row2['phoneNumber'] . " " . $row2['extension'];
}
$results['phoneNumber'] = $phoneNumbers;
}
$data['title']="Page Title Goes Here";
$data['username']="Your Username";
$this->load->view('header', $data);
$this->load->view('people_results', $results);
$this->load->view('footer');
}
}
Here is the people_model class:
function __construct()
{
// Call the Model constructor
parent::__construct();
}
function get_all_contacts() {
$query = $this->db->query('SELECT * FROM person');
return $query->result();
}
function get_phone_by_id($id) {
$query = $this->db->query('SELECT * FROM phoneNumber WHERE personID = '.$id);
return $query->result();
}
}
The only thing I might question in database.php is if the hostname needs a port number, but I don't think that helped when I tried it.
I just tried adding (based on similar question in side bar)
$this->mydb = $this->load->database('realDB', TRUE);
to the people_model and changing the db to mydb and received this error:
You have specified an invalid database connection group.
and this is my line in database.php:
$db['default']['database'] = 'realDB';
Thanks for all your help.
since get_all_contacts is already using the result() function in your model, you can't also use the result_array() function in your controller. result_array() would be a method of the $query object. The quick and dirty way to get this working(which might break other stuff if its also using the get_all_contacts method) would be to change your get all contacts function to the following:
function get_all_contacts() {
$query = $this->db->query('SELECT * FROM person');
return $query;
}
however, if you want to be smarter about it and not risk breaking other stuff, you can pass a param from the controller, and only return query if its set like so:
REVISED CONTROLLER LINE**
$allContacts = $this->people_model->get_all_contacts(true);
REVISED MODEL CODE
function get_all_contacts($special = false) {
$query = $this->db->query('SELECT * FROM person');
if($special)
{
return $query;
}
return $query->result();
}

Categories