CodeIgniter Forums
query method return false? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: query method return false? (/showthread.php?tid=43266)

Pages: 1 2


query method return false? - El Forum - 07-06-2011

[eluser]nourdine[/eluser]
hello

I am reading the docs about

Code:
$this->db->query();

It says that
Quote:When "write" type queries are run it simply returns TRUE or FALSE depending on success or failure

Now my question is:

Under what circumstances would the query() method return false when performing a writing query?

Like, lets say I do

Code:
$sql = "UPDATE notexistingtable SET active = true WHERE id = 123";
$this->db->query($sql)

this will fail but not at the method level. The whole thing will blow up and the method will just never return. So I cant really figure out when this method returns false.

any ideas?


query method return false? - El Forum - 07-06-2011

[eluser]mi6crazyheart[/eluser]
Code:
$sql = "UPDATE notexistingtable SET active = true WHERE id = 123";
$this->db->query($sql);

What u want to ask i can't understand but if these are the statements, then when the 2nd line will execute that'll return the FALSE...


query method return false? - El Forum - 07-06-2011

[eluser]nourdine[/eluser]
I think I tried that and got an error before query() returned ...

I was expecting a false but execution stopped beforehand. I will try again.


query method return false? - El Forum - 07-06-2011

[eluser]C. Jiménez[/eluser]
your model's method will not return false.
Code:
$sql = "UPDATE notexistingtable SET active = true WHERE id = 123";
        $result = $this->db->query($sql);  
        var_dump($result);
$this->db->query($sql) will return sucess or failure state as bool.

If you want your model's method to return false if query is wrong just handle $result with return.

Code:
$sql = "UPDATE notexistingtable SET active = true WHERE id = 123";
        if($this->db->query($sql) !== FALSE)
        {
          //what to do when isn't wrong.
          return TRUE;
         }
        return FALSE;

[/code]


query method return false? - El Forum - 07-06-2011

[eluser]marjune[/eluser]
Code:
$sql = "UPDATE notexistingtable SET active = true WHERE id = 123";
$this->db->query($sql);

this function return false when your mysql function or sql query has a syntax error, otherwise its always return true


query method return false? - El Forum - 07-06-2011

[eluser]C. Jiménez[/eluser]
Quote:this function return false when your mysql function or sql query has a syntax error, otherwise its always return true
"UPDATE notexistingtable SET active = true WHERE id = 123" is syntactically correct, but notexistingtable doesn't exist.

and example i post prints "bool(false) "


query method return false? - El Forum - 07-06-2011

[eluser]marjune[/eluser]
if your looking the result in literal then it look like this

Code:
$sql    = "UPDATE notexistingtable SET active = true WHERE id = 123";
$result = $this->db->query($sql);
if($result)
  return true;//or what ever you return here or maybe you display something like echo "true";
else
  return false;//or what ever you return here or maybe you display something like echo "false";



query method return false? - El Forum - 07-06-2011

[eluser]C. Jiménez[/eluser]
just create a new default empty controller and write index method like this
Code:
function index(){
        $sql = "UPDATE notexistingtable SET active = true WHERE id = 123";
        $result = $this->db->query($sql);  
        var_dump($result);
        
    }
And in your browser you will see $result value and content:

"bool(false) "


And as I said:
Quote:“UPDATE notexistingtable SET active = true WHERE id = 123” is syntactically correct, but notexistingtable doesn’t exist.



query method return false? - El Forum - 07-06-2011

[eluser]jmadsen[/eluser]
in order for this to work, you must go into

config/database.php

$db['default']['db_debug'] = TRUE;

and set it to FALSE;

If not, the DB_driver.php function will return the database error to the page, and "crash" it


query method return false? - El Forum - 07-06-2011

[eluser]Caio[/eluser]
why would you want your model's method to return false?! that doesn't make sense for me, how are you going to debug later on?!