Error in fetch() after an executed sql query
Imagine the code
$sql1 = "Simple SQL query INSERT";
$row = $db->query($sql1)->execute();
$sql2 = "Simple SQL query Select";
$row = $db->query($sql2)->fetch();
After the first SQL1 execute $this -> _executed is marked as TRUE Then when we call the second fetch it will give an sql error because on fetch it verifies that sql is executed and dont execute again the new SQL so we need to do.
$sql1 = "Simple SQL query INSERT";
$row = $db->query($sql1)->execute();
$sql2 = "Simple SQL query Select";
$row = $db->query($sql2)->execute();
$row = $db->query($sql2)->fetch();
Comments (5)
-
-
I can confirm this, needs to be fixed.
-
I found out that this can be fixed by adding "$this -> _executed = FALSE;" to the query function:
public function query($query, $sanitize = FALSE) { $this -> _executed = FALSE; if ($sanitize == TRUE) $this -> _query = filter_var($query, FILTER_SANITIZE_STRING); else $this -> _query = $query; return $this; }
-
I'm not totally sure that my explanation below is applicable to Anonymous problem. But maybe to yours Joel Stanford.
You'll get an Notice depending on your error reporting level. This is because of the _query property is unset and destroyed after each execute. This is normal that PHP would react and give a notice about that because of the way that code is written.
If we take a look at
public function fetch() { if ($this -> _executed == FALSE || !$this -> _query) $this -> execute();
and
public function fetch_first() { if ($this -> _executed == FALSE || !$this -> _query) $this -> execute();
In both functions above the _query does not exist in this point of time assuming there has been another execution before calling the fetch. So PHP is reporting an notice about that fault. There are different way to solve this problem but one is to modify the code to:
if ($this -> _executed == FALSE || property_exists('Database', '_query')) $this -> execute();
"Database" is the class name in the function property_exists;
-
repo owner - changed status to resolved
I really appreciate for the suggestions. Thanks guys for the heads up. I was able to duplicate the issue and the suggestion from @NoXPhasma has been adopted.
Version 1.4.4 has been uploaded with the bug fix and new method find_in_set has also been added.
- Log in to comment
I'm also having this issue - after an insert() or update() call, fetch() or fetch_first() will result in a _query being not set, or an error saying 'Unable to perform fetch_first()'