Codeigniter SQL query return single value [duplicate] - php

This question already has answers here:
getting the value of the single field output using the codeigniter active record
(5 answers)
Closed 2 years ago.
I am working with CodeIgniter and I want to get the highest ID out of a table. This i realize with a function in the Model. But I don't get the result, which is a single value, as value back.
Code:
function get_highest_answer_id(){
$this->db->query('SELECT MAX(id) AS answerid FROM pa_it_answer;');
if ($query = $this->db->get()) {
if ($query->num_rows() > 0) {
return $query->result();
} else {
return array();
}
} else {
return FALSE;
}
}
How do I have to Change the code that when I call this function in my Controller I get the single value?

Below code is works fine for me.
$MAXID = 0;
$row = $this->db->query('SELECT MAX(id) AS answerid FROM pa_it_answer')->row();
if ($row) {
$MAXID = $row->answerid;
}

Change your query in codeigniter like this
function get_highest_answer_id(){
$this->db->select('MAX(id) AS answerid');
if ($query = $this->db->get('pa_it_answer')) {
if ($query->num_rows() > 0) {
return $query->result();
} else {
return array();
}
} else {
return FALSE;
}
}

You just have to simplify your method with row()
function get_highest_answer_id(){
$record = $this->db->query('SELECT MAX(id) AS answerid FROM pa_it_answer;')->row();
if ($record) {
return $record->answerid;
}
else {
return FALSE;
}
}

Try this:
function get_highest_answer_id(){
$this->db->query('SELECT MAX(id) AS answerid FROM pa_it_answer;');
if ($query = $this->db->get()) {
if ($query->num_rows() > 0) {
return $query->row(); // here!!!
} else {
return array();
}
} else {
return FALSE;
}
}
I recommended use the querybuilder of CI (EDITED)
$this->db->select_max('id', 'answerid');
$query = $this->db->get('pa_it_answer');
var_dump( $query );
var_dump( $query->answerid );
die();

Related

Message: Cannot use object of type stdClass as array in CodeIgnitor

I am using CodeIgniter, I am returning more than one value in the model so I did like
return array('true' =>1,'customer_id' => $result->customer_id);
and In the controller, I am displaying like
$true=$result['true'];
$custId=$result['customer_id'];
So there is no issue in this.
Now let's talk about in details
In the model, I have this logic
//sql query
if ($result) {
if ($result->password_change == 0) {
return array('true' =>1,'customer_id' => $result->customer_id);//multiple returns
}
else{
return $result;
}
}
else{return false;}
In the controller
$result=$this->Member_model->check_data(parameters);
$true=$result['true'];
$custId=$result['customer_id'];
if ($result) {
if($true == 1){
//some code here
}
else{
//some code here
}
}
else{
}
This is my second code.
if ($result) {
if ($result - > password_change == 0) {
if ($result - > id == $id) {
return array('true' => 1, 'customer_id' => $result - > customer_id); //return more then 2
} else {
return false;
}
} else {
if ()) // some condition
{
return (array) $result;
} else {
return false;
}
}
} else {
return false;
}
I am getting the error that
"Message: Cannot use object of type stdClass as array"
Because when returning if condition(from the model) then it's working but when it returns else (I mean $result from the model) then I am getting the error because it is not getting the $result['true'].
Hope you can understand my issue. Would you help me out on this issue?
You have two options. Either: ALWAYS return an array, or return an ArrayObject:
Alway return an Array
In your else, cast $result as an array:
//sql query
if ($result) {
if ($result->password_change == 0) {
return array('true' =>1,'customer_id' => $result->customer_id);//multiple returns
}
return (array) $result;
}
return false;
Always return an ArrayObject
Not knowing if this model is returning the result anywhere else in you application, I have no idea if other code expects $result to be an object. Casting as an array might break your code elsewhere. Let's convert $result from stdClass to an ArrayObject:
//sql query
if ($result) {
if ($result->password_change == 0) {
return array('true' =>1,'customer_id' => $result->customer_id);//multiple returns
}
return new \ArrayObject($result, \ArrayObject::ARRAY_AS_PROPS);
}
return false;
This approach will allow you to call $result['test'] AND $result->test.
Yeah, ok this is solution for multiple conditions :
$this->db->select('*'); // Here your column name
$this->db->from('members'); // Table name
$this->db->where($login_access); // Here your condition
$query = $this->db->get();
$result = $query->result_array();
$query = $this->db->get('customer'); // Your code above if condition
$result = $query->result_array(); // Get data in array Format using **result_array()**
if ($result) {
if ($query->num_rows() > 0) { // You have to change condition from **$result['password_change ']** To **$query->num_rows() > 0**
if ($result - > id == $id) { // Also change here condition from **$result - > id** To $result['id']
return array('true' => 1, 'customer_id' => $result['customer_id']); //Also change here from **$result - > customer_id** To $result['customer_id']
} else {
return false;
}
} else {
if () // some condition
{
return $result; // Also Change here from **return (array) $result** To $result
} else {
return false;
}
}
} else {
return false;
}

How to check 2 values in 1 table using PHP, jQuery?

Is there a way to check 2 exists values in 1 table ?
It was work just fine when i use only 1 value 'beezName' but when i added another value 'divId' it didn't work, they only check whether the 'beezName' or 'divId' exist in table. I'm trying to get :
if beezName and divId exist then return true else false
How can i make this work? Thanks in advance
Here's my controller : Beez
function checkBeezExists()
{
$beezId = $this->input->post("beezId");
$beezName = $this->input->post("beezName");
$divId = $this->input->post("divId");
if(empty($beezId)){
$result = $this->beez_model->checkBeezExists($beezName, $divId);
} else {
$result = $this->beez_model->checkBeezExists($beezName, $divId, $beezId);
}
if(empty($result)){ echo("true"); }
else { echo("false"); }
}
And this is my model : beez_model
function checkBeezExists($beezName, $divId, $beezId= 0)
{
$this->db->select("beezName, divId");
$this->db->from("tbl_beez");
$this->db->where("beezName", $beezName);
$this->db->where("divId", $divId);
$this->db->where("isDeleted", 0);
if($beezId != 0){
$this->db->where("beezId !=", $beezId);
}
$query = $this->db->get();
return $query->result();
}
You want to check beezName and divId exist but in your model you have wrong condition for beezId
try below code:
function checkBeezExists($beezName, $divId, $beezId= 0)
{
$this->db->select("beezName, divId");
$this->db->from("tbl_beez");
$this->db->where("beezName", $beezName);
$this->db->where("divId", $divId);
$this->db->where("isDeleted", 0);
if($beezId != 0){
$this->db->where("beezId", $beezId); //<--------remove !=
}
$query = $this->db->get();
return $query->result();
}

Recursive function return null value [duplicate]

This question already has answers here:
How to use return inside a recursive function in PHP
(4 answers)
Closed 9 months ago.
I have Used recursive function to get the category full path like Access Control/CARDS/FOBS from above table structure but my recursive function return null value.
function xyz($id,$parent) {
if($parent == '0') {
//my code working fine
//return
}
else
{
$catid = $id; //here 25 coming
$cat_array = array();
$category_array = $this->Recursive($catid,$cat_array);
//echo $category_array;exit; getting Null result
return $category_array ;
}
}
function Recursive($catid,$cat_array)
{
$sql = mysql_query("Select bg_category_id,parent_id,title from categories_list
Where bg_category_id = '".$catid."'");
$result = mysql_fetch_array($sql);
array_push($cat_array,$result['title']);
if($result['parent_id'] != '0') {
$this->Recursive($result['parent_id'],$cat_array) ;
} else {
if(count($cat_array) > 0){
$k = implode("/",$cat_array);
}
//echo $k;exit; getting desired result FOBS/CARDS/Access Control
return $k;
}
}
You need to return the recursive function when you recurse, otherwise it will return nothing.
if($result['parent_id'] != '0') {
return $this->Recursive($result['parent_id'],$category_array) ;
}

Codeigniter active record statement not running when sql query doesn't return anything

I seem to be having a problem with the below code. I cannot get the insert query to run when the query returns nothing. But when there is a row in the db, the else statement runs correctly. Does anybody know why this could be happening?
$query5 = $this->db->get_where('loggedstatus', array('userid_loggedstatus' => $userid));
if ($query5->num_rows() < 0) {
$data1 = array('isLoggedIn_loggedstatus' => 'yes', 'userid_loggedstatus' => $userid);
$this->db->insert('loggedstatus', $data1);
} else {
$data = array('isLoggedIn_loggedstatus' => 'yes');
$this->db->where('userid_loggedstatus', $userid);
$this->db->update('loggedstatus', $data);
}
Have you tried changing this if ($query5->num_rows() < 0) { to something like if ($query5->num_rows() <= 0) {, just a thought. It would make a difference because your telling it to only execute if its less than zero however it might be equal to zero and you would skip to the else statement.
And just for CodeIgniter num_rows() reference:
/**
* Number of rows in the result set
*
* #return int
*/
public function num_rows()
{
if (is_int($this->num_rows))
{
return $this->num_rows;
}
elseif (count($this->result_array) > 0)
{
return $this->num_rows = count($this->result_array);
}
elseif (count($this->result_object) > 0)
{
return $this->num_rows = count($this->result_object);
}
return $this->num_rows = count($this->result_array());
}
Changing if condition shown below should fix the problem.
if ($query5->num_rows() == 0)

How to return true with a recursive function [duplicate]

This question already has answers here:
How to use return inside a recursive function in PHP
(4 answers)
Closed 9 months ago.
I have below recursive function in my model with CI, working.
In my controller, i need to check if function worked correctly like:
if($this->my_model->level_corrector($id_page,$level)) echo 'Levels are corrected';
But as the function always return false (to end the recursion), I couldn't figure out how to achieve my goal.
function level_corrector($id_page_of_parent,$level_of_parent)
{
$sql = "
SELECT id_page, id_parent, level
FROM page
WHERE id_parent = $id_page_of_parent";
$query = $this->db->query($sql);
if($query->num_rows() > 0)
{
$result = $query->result_array();
foreach ($result as $r)
{
$data = array('level'=>$level_of_parent+1);
$this->db->where('id_page', $r['id_page']);
if($this->db->update('page', $data))
{
$this->level_corrector($r['id_page'],$level_of_parent+1);
}
else
{
// let me handle it what to do
return false;
}
}
}
else
{ // again let me handle it to log a message or sth
return false; // (2)
}
return true; // (3) means it all gone right, so I can move on.
}
You need to do two things:
If there is an error, return false. Otherwise, at the end of the function, return true by default
If, when you call the function recursively, an error occurred, return false.
Edit: Based on your answers to my questions in the comments, what you want is this:
function level_corrector($id_page_of_parent,$level_of_parent)
{
$sql = "
SELECT id_page, id_parent, level
FROM page
WHERE id_parent = $id_page_of_parent";
$query = $this->db->query($sql);
if($query->num_rows() > 0)
{
$result = $query->result_array();
foreach ($result as $r)
{
$data = array('level'=>$level_of_parent+1);
$this->db->where('id_page', $r['id_page']);
if($this->db->update('page', $data))
{
// no error. return error code from recursive call
return $this->level_corrector($r['id_page'],$level_of_parent+1);
}
else
{
// error occured
return false;
}
}
}
// $query->num_rows <= 0. This is not an error, so return true:
return true;
}

Categories