Welcome Guest, Not a member yet? Register   Sign In
query method return false?
#1

[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?
#2

[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...
#3

[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.
#4

[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]
#5

[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
#6

[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) "
#7

[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";
#8

[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.
#9

[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
#10

[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?!




Theme © iAndrew 2016 - Forum software by © MyBB