Why is PHP using the Pdo->exec () method to verify that the data exists before inserting the data with the exec () query, which leads to the subsequent exec () Insert data failure?
When inserting data, it is well understood that repeated insertions of the same data lead to failure. But why did you use EXEC () query before inserting, and the data does not exist and also leads to exec () failed to insert data?
Reply to discussion (solution)
Post your code and make a note
A total of 22 puzzling places, respectively, in different ways, the first fragment is the Add () method, that is, the encapsulated insert data method.
A few bugs have just been fixed, the one that executes exec () after executing exec () Insert returns 0 The problem seems to be a black dragon, but the problem persists. That is, the EXEC () query returns 0, but the data is clearly present.
The second paragraph is the data deletion method, the same problem, also with the exec () when the return value is 0, but the data clearly exists. The SQL statement does not seem to have any problem.
Case ' PDO ': $PDO = $this->connect;//pdo instantiation of the object try {# $isQuery ===1 when querying validation if ($isQuery ===1) {$QSQL = ' select '. $strQTitle. ' From '. $strTable. ' WHERE '. $strQTitle. ' = "'. $Value. '"; /select id,enname from Mh_forumpage where enname= "Egfdfa" $QuerySQL = $PDO->exec ($QSQL); Note: If you use EXEC to query a statement that is not found and then insert this data, the insertion failure will occur. $QuerySQL = $this->query ($strQTitle, $strTable, $strQTitle, $Value); if ($QuerySQL!==0 or $QuerySQL!=false) { / /********* is executed here after the execution of the program, because the $querysql return value is int (0), but the data is clearly present. Die ('
'. __class__. ' Class '. __function__. ' () method hint error: The value to be added already exists. or an error in the SQL statement. ');}} #添加语句 $EXEC = $PDO->exec ($SQL),//SQL execution with no result set using EXEC (), if ($EXEC ===0 or $EXEC ==false) {//* Returns an equal to FALSE when SQL statement execution fails, such as Nullreturn FALSE;} Else{return $EXEC;//delete succeeded, return the affected number}} catch (Exception $e) {echo ' Error message: '. $e->getmessage ();} Break;default:die ('
'. __class__. ':: '. __function__. ' Class hint error: Invalid type of database ');}
Second paragraph: delete data
Case ' PDO ': $PDO = $this->connect;try {#查询验证, its query value is not double-quoted because the number or character has already been processed before. Normally, EXEC returns a number that returns a value equivalent to False when an error occurs on the SQL statement. $QSQL = ' select '. $strQTitle. ' From ' $strTable. ' WHERE '. $strQTitle. ' = '. $Value; $QueryExec = $PDO->exec ($QSQL); Var_dump ($QueryExec); if ($QueryExec ===0 or $QueryExec ==false) { //** The same will be done here, because the $queryexec return value is 0die ('
'. __class__. ':: '. __function__. ' Class hint error: The data to be deleted does not exist. ');} #删除数据, note that deletion failure will return 0 instead of false$exec= $PDO->exec ($SQL);//SQL execution with no result set using EXEC (); if ($EXEC ===0 or $EXEC ===false) {return FALSE;} Else{return $EXEC;//delete succeeded, return the affected number}} catch (Exception $e) {echo ' Error message: '. $e->getmessage ();} Break
Pdo::exec () returns the number of rows that were modified or deleted by the SQL statement you issued. If no rows were affected, pdo::exec () returns 0
Pdo::exec () returns the number of rows affected by your SQL instruction (modified or deleted), or 0 if no rows are affected
So when you use Pdo::exec () to execute a query instruction, it will only return 0 because no rows are modified or deleted.
$QSQL = ' select '. $strQTitle. ' From ' $strTable. ' WHERE '. $strQTitle. ' = '. $Value; $QueryExec = $PDO->exec ($QSQL); Var_dump ($QueryExec); if ($QueryExec ===0 or $QueryExec ==false) {die ('
'. __class__. ':: '. __function__. ' Class hint error: The data to be deleted does not exist. ');}
Sorry, I'm a little messed up. The previous two pieces of code are the problem here, that is, the Exec () method is used. The first code is the EXEC () query, but EXEC (insert) returns FALSE, and if EXEC (query) is removed, then EXEC (insert) is executed successfully. What is this situation ...
At the same time, the second code in the Exec (query) return is indeed an int (0), which should actually return int (1). Because the data exists, does it exist to be deleted? So there are two problems, which are estimated to be a fault. SQL statements seem to have no problem.
SQL statement for the first paragraph of code:
Select Id,enname from Mh_forumpage where enname= "Egfdfa"
This is the SQL statement that EXEC () executes when querying before inserting data. The database and field names are right.
In fact, if you delete and add data before the operation of the query verification is also able to meet the normal function, but do not understand why this. Used to be mysqli, with PDO just soon.
Pdo::exec () returns the number of rows that were modified or deleted by the SQL statement you issued. If no rows were affected, pdo::exec () returns 0
Pdo::exec () returns the number of rows affected by your SQL instruction (modified or deleted), or 0 if no rows are affected
So when you use Pdo::exec () to execute a query instruction, it will only return 0 because no rows are modified or deleted.
In this sense, the first code of EXEC () returns 0 which is correct, because this is the query validation before inserting, and returning 0 is correct. However, the subsequent EXEC (insert) returns FALSE, and if the previous query validation is removed, then exec (insert) works. Don't understand.
The second code is the delete operation, the query verification before deletion, using the exec (), but the return value is 0, the data exists, should return 1.
Pdo::exec () returns the number of rows that were modified or deleted by the SQL statement you issued. If no rows were affected, pdo::exec () returns 0
Pdo::exec () returns the number of rows affected by your SQL instruction (modified or deleted), or 0 if no rows are affected
So when you use Pdo::exec () to execute a query instruction, it will only return 0 because no rows are modified or deleted.
Oh.. That is, when executing the SELECT statement, whether the query is successful or not, returns 0? In that case, no result set query can be used by exec ().
That's right!
The SELECT command cannot be executed with exec.
If it returns false, it means that the instruction is wrong.
You can guide what the problem is by looking at the results returned by the ErrorInfo method
That's right!
The SELECT command cannot be executed with exec.
If it returns false, it means that the instruction is wrong.
You can guide what the problem is by looking at the results returned by the ErrorInfo method
Thank you, and reduce a misunderstanding, I said with the Exec () method has been a strange problem, so to speak. Thank you so much.