Sunday, September 23, 2018

php - Prepared statements and bind_param error handling

In the following php code with prepared statements is intentionally caused an error to test bind_param and execute, adding $bikes.




In the execute function the error information is returned; however, in bind_param, although an error occurs, it does not return any error information.



How to get error information in bind_param?



$sqlQuery = "INSERT INTO the_cars (cars) VALUES (?)";

if($statement = $con->prepare($sqlQuery)){

if(!$statement->bind_param("s", $cars, $bikes)){ //bikes should not be here
$errors = $statement->error; //error is empty

};

if(!$statement->execute()){
$errors1 = $statement->error; // error: No data supplied for parameters in prepared statement
};

}else{
$errors = $con->error;
}



EDITED:



The PHP manual seems to suggest that in bind_param the error should be handled.
See the following part of the text: Example #3 INSERT prepared once, executed multiple times » /* Prepared statement, stage 2: bind and execute */



http://php.net/manual/en/mysqli.quickstart.prepared-statements.php



This also seems to be the position advocated in some posts. For example:




MySQLi prepared statements error reporting



However, I made several attempts, and I was never able to get a description of a statement error in bind_param.

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...