1

I am new to Codeigniter and PHP..

while doing some operations i have some doubts ,especially in the database libraries .

$get = "select filed_1 from tbl_ctc where ctc=?";
$get = $this->db->query($get,array($ctc_n));

if ($get)
{

 //do some operations 
}
else
{
 //do some another operations 

}

What will be the return value of $get if it is success ?

Expecting opinion & suggestion [downvotes too]

Thank you.

2 Answers 2

4

What "opinions and suggestions" do you expect?

The query() method returns a result object when you use read queries (like a select), while with write queries (like an insert or update) it returns TRUE or FALSE.

In case of a SELECT, i.e., you might want to check if it has any result:

$query = $this->db->query($select_query);
if($query->num_rows() > 0)
{}

While, in case of an INSERT for ex.:

$query = $this->db->query($insert_query);
if($query)   // or if(FALSE !== $query)
{}
else
{}

Note that if you are using Active Record, you'll have the result() / row() and result_array() / row_array() methods that will return either a full object/array or an empty one, so you'll need to check for those values instead.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you,this is what i exactly expected answer,but what will return if the query failed in some cases[in select queries]....
"opinions and suggestions" - about coding practices ,beginner mistakes ,good links to sources etc
Well, I cannot test right now, but simply var_dump() the returned value of a failing query (if you can create it somehow).I suspect an empty object or a FALSE, but never tried that.
1

The result will be an object. You have to go one step further to get the expected value, e.g.

$get = $this->db->query($get,array($ctc_n));
$res = $get->result_array();

echo $res[0]['filed_1'];

See Active Record Class and examples from the user guide for further reference.

6 Comments

yeah..sure i can use row_array too ,but i have trouble with that IF loop can i do like this if ($get) ??
@DileepDil -- If you just want to see if a record(s) has been returned, you could use if ($get->num_rows() > 0) {}
Okkey,but i think that it will return FALSE if the query failed ,so can i use if($get != FALSE) ?? also what about insert functionality ??
What do you mean if the query failed? If no records were found, if ($get) {} will still return as true...it's not a reliable way of checking if there has been a result or not. As for insert functionality, you can use if ($this->db->insert('yourtable', $data)) {}, which will return true if the query was successful.
I am not saying about what is returned through the query,i just want to know that the Query operation is success or failed ,
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.