0

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 "<br/>Erro: " . $conn->error;

And execute it that way(snippet from a function)

                if(!$p['GetAdmUsers']->bind_param('i', $id))
                echo "<br/>Erro: ". $c->error;
            if(!$p['GetAdmUsers']->execute())
                echo "<br/>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

Shadow
  • 33,525
  • 10
  • 51
  • 64
Conflux
  • 23
  • 5
  • I think you have to use `if(!$p['GetAdmUsers']->bindParam(1, $id))' and not 'i'. I don't try it, but this seems the problem to me. – skroczek Mar 01 '15 at 06:59
  • Confused by your code. If `$p['GetAdmUsers']` is the statement, you have to extract the error code (`errno`) and error string (`error`) from that statement, not whatever `c` is. – BaseZen Mar 01 '15 at 07:03
  • I tried it, but as i expected it returned that error: "Undefined fieldtype 1" Now I'm curious about why you thought about that solution, can you explain, please? – Conflux Mar 01 '15 at 07:13
  • $c is the mysqli connection object, errno is 0, sorry for not explaining that, I'll edit it – Conflux Mar 01 '15 at 07:16

1 Answers1

0

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 "<br />" . $p['GetAdmUsers']->error;
BaseZen
  • 8,650
  • 3
  • 35
  • 47
  • It worked, now i get the error "Commands out of sync; you can't run this command now", what is probably because I'm executing the query inside a $stmt->fetch() loop, thanks. – Conflux Mar 01 '15 at 07:34