Object of class CI_DB_mysqli_result cannot be converted to string
here is my code:
class Mymodel extends CI_Model {
public function getinstitution()
{
$course = "programming";
$location = "jakarta";
$price = "price2";
$data = $this->db->query('SELECT * FROM coursesplace WHERE 1=1');
if($course) $data .= "AND course=\"$course\" ";
if($location) $data .= "AND location=\"$location\" ";
if($price) $data .= "AND price=\"$price\"";
return $data->result_array();
}
}
I want to filter computer course locations based on three variables (course, location and price) with a string data type. For example, I have "programming", "jakarta" and "price2".
But then I have this error:
PHP error occured
Severity: 4096
Message: Object of class CI_DB_mysqli_result could not be converted to string
File name: models / mymodel.php
Line number: 14
---> Line 14:
if($course) $data .= "AND course=\"$course\" ";
And another error:
PHP error occured
Severity: error
Message: Calling member function result_array () on non-object
File name: models / mymodel.php
Line number: 18
---> Line 18:
return $data->result_array();
What should I do? Thanks for the answer!
source to share
You can just get rid of the quotes like
$data = 'SELECT * FROM coursesplace WHERE 1=1';
if ($course)
$data .= " AND course='$course' ";
if ($location)
$data .= " AND location='$location' ";
if ($price)
$data .= " AND price='$price'";
$result = $this->db->query($data);
return $result->result_array();
Active records ..
public function getinstitution() {
$course = "programming";
$location = "jakarta";
$price = "price2";
$this->db->select('*');
$this->db->from('coursesplace');
$this->db->where('1 = 1');
if ($course != '') {
$this->db->where('course', $course);
}
if ($location != '') {
$this->db->where('location', $location);
}
if ($price != '') {
$this->db->where('price', $price);
}
$data = $this->db->get()->result_array();
return $data;
}
source to share
$ data is an object that you cannot concatenate a string for an object.
$query = "SELECT * FROM coursesplace WHERE 1=1"
if(isset($course)) $query .= " AND course=\"$course\" ";
if(isset($location)) $query .= " AND location=\"$location\" ";
if(isset($price)) $query .= " AND price=\"$price\" ";
$data = $this->db->query($query);
This will help. I took the variable $ query and assigned a string to it according to the conditions the string is concatenated. And at the end the final query gives $ this-> db-> query
source to share
Your problem is that you are executing a request and then trying to modify the request.
You must add "SELECT * FROM place rates WHERE 1 = 1" to the $ data line, for example:
$data = 'SELECT * FROM coursesplace WHERE 1=1';
// Then ammend the $data
if($course) $data .= "AND course=\"$course\" ";
if($location) $data .= "AND location=\"$location\" ";
if($price) $data .= "AND price=\"$price\"";
// Now run the query
$query = $this->db->query($data);
return $query->result_array();
source to share