I have the following PHP code:
$inserted = false ;
printf("Record must be inserted. Old DB ID is %d.\n", $old_db_id) ;
$insertSQL = "INSERT INTO GoogleAuth ("
. "script"
. ", client_id"
. ", client_secret"
. ", refresh_token"
. ", auth_token"
. ", refresh_token_date"
. ", auth_token_date"
. ", client_info_date"
. ", last_used_date"
. ")"
. " VALUES(?, ?, ?, ?, ?, ?, ?, ?, NULL) "
;
printf("Preparing statement....\n") ;
$stmt = $mySQL->prepare($insertSQL) ;
if ($stmt) {
printf("Binding parameters...\n") ;
if ($stmt->bind_param("sssssiiii"
, $page_file
, $clientId
, $clientSecret
, $refreshToken
, $authToken
, $authTokenDate
, $refreshTokenDate
, $clientInfoDate
, $now
)) {
print("Bind returned true\n") ;
if ($stmt->execute()) {
$inserted = true ;
}
}
else {
printf("Bind failed. %s\n", $stmt->error) ;
}
}
if ($inserted) {
printf("row inserted.\n") ;
}
else {
printf("Insert failed! %s\n", $mySQL->error) ;
trigger_error("Insert failed. Dang it!", E_USER_ERROR) ;
}
When I execute this code, the following is printed:
Record must be inserted. Old DB ID is 0.
Preparing statement....
Binding parameters...
Bind failed.
Insert failed!
Note that the $stmt->error value and the $mySQL->error values are both empty!
Can you please help me understand how to reliably obtain the error message (and/or error code) associated with these sorts of failures? (I eventually figured out that I was putting 9 variables in place of 8 replacement markers... that's not the point. The point is to figure out how to get the actual errors from mysql.)
Thanks!
EDIT
By the way, I've seen articles like this that talk about interesting reuse of variable names. That does not apply here. $stmt is not used anywhere else in this application.
EDIT2
It was suggested that this is a duplicate of this problem. However, the difference is that the goal here is to obtain the failure message so that it can be analyzed for proper handling. (Some errors may be manageable, depending upon circumstances.) In any event, such process makes it possible to GRACEFULLY close down a program, rather than the rude and immediate result that comes from throwing failure errors. As I read the other problem, the goal is to ensure that an error gets to the log, and the program gets aborted.