Thursday, 24 August 2017

mysql - PHP MySQLI prepared statement returning false on execution




So, i have a prepared statement that returns false on execution but error code is 0, meaning no error, despite that there's obviously an error since it's not being executed..



I prepared the statement that way:



if(!$prepared_statements['GetAdmUsers'] = $conn->prepare('SELECT nome FROM Alunos WHERE professor = ?'))
echo "
Erro: " . $conn->error;


And execute it that way(snippet from a function)




                if(!$p['GetAdmUsers']->bind_param('i', $id))
echo "
Erro: ". $c->error;
if(!$p['GetAdmUsers']->execute())
echo "
Erro: ". $c->error;


Before you ask me for the full code, i should say that all variables and objects were tested and that's not the problem, other parts of the code other than used variable and objects are not connected to the issue.



Thanks in advance.




edit:
$c is the mysqli connection object, errno is 0


Answer



You are extracting the error from the wrong object. The error is stored inside the statement object, not the connection object. See:



http://php.net/manual/en/mysqli-stmt.error.php



/* execute query */
$stmt->execute();


printf("Error: %s.\n", $stmt->error);


Note that the code is using the same object is used to execute the statement and to retrieve the error. You need to use, in your case:



echo "
" . $p['GetAdmUsers']->error;

No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...