When PHP uses the PDO-> exec () method, it uses exec () to query and verify whether the data exists before it is inserted. it fails to insert data to the exec () method.

Source: Internet
Author: User
When PHP uses the PDO-& gt; exec () method, it uses exec () to query and verify whether the data exists before it is inserted () failed to insert data: PHP used the PDO-> exec () method to query and verify whether the data exists before the data is inserted by exec (). why?
When you insert data, repeated insertion of the same data leads to failure, which is understandable. But why do I use exec () to query the data before insertion and the data does not exist? it still leads to exec () data insertion failure?


Reply to discussion (solution)

Post your code and describe it

In total, the two are confusing. The first segment is in the Add () method, that is, the encapsulated data insertion method.
Several bugs have just been fixed. the problem that 0 is returned after exec () is executed and exec () is inserted, but the problem persists. That is, when exec () is queried, 0 is returned, but the data clearly exists.
The second part is the data deletion method. the same problem is that when exec () is used, the return value is 0, but the data obviously exists. The SQL statement does not seem to see any problems.

Case 'pdo': $ pdo = $ this-> Connect; // when the pdo instantiated object try {# $ isQuery = 1, perform query verification if ($ isQuery = 1) {$ QSQL = 'select '. $ strQTitle. 'from '. $ strTable. 'Where '. $ strQTitle. '= "'. $ Value. '"'; // select ID, ENName from mh_forumpage where ENName =" egfdfa "$ QuerySQL = $ PDO-> exec ($ QSQL); # description: if you use exec to query a statement that is not found and then insert the data, insertion will fail. // $ QuerySQL = $ this-> Query ($ strQTitle, $ strTable, $ strQTitle, $ Value); if ($ QuerySQL! = 0 or $ QuerySQL! = FALSE) {// ********** after the program is executed, it will be executed here, because the returned value of $ QuerySQL is int (0), but the data clearly exists. Die ('
The '. _ CLASS _.' CLASS '. _ FUNCTION _.' () method prompts an error: the value to be added already exists. Or an SQL statement error occurs. ') ;}# Add statement $ EXEC = $ PDO-> exec ($ SQL); // execute an SQL statement without a result set using exec (); if ($ EXEC = 0 or $ EXEC = FALSE) {// when an SQL statement fails to be executed, return the same type as FALSE, for example, NULLreturn FALSE ;} else {return $ EXEC; // deletion successful, return number of affected items} catch (Exception $ e) {echo 'error message :'. $ e-> getMessage ();} break; default: die ('
'. _ CLASS _.': '. _ FUNCTION _.' CLASS error: Invalid database type ');}


Section 2: delete data
Case 'pdo': $ pdo = $ this-> Connect; try {# Query verification. do not add double quotation marks to the query value, because the number or character has been processed before. Under normal circumstances, exec will return a number, and when an SQL statement error occurs, it will return a value equivalent to FALSE. $ QSQL = 'select '. $ strQTitle. 'from '. $ strTable. 'Where '. $ strQTitle. '= '. $ Value; $ QueryExec = $ PDO-> exec ($ QSQL); var_dump ($ QueryExec); if ($ QueryExec = 0 or $ QueryExec = FALSE) {// ************* will also be executed here, because the returned value of $ QueryExec is 0die ('
The '. _ CLASS _.': '. _ FUNCTION _.' CLASS prompts an error: the data to be deleted does not exist. ') ;}# Delete data. Note that 0 is returned if the deletion fails, instead of FALSE $ EXEC = $ PDO-> exec ($ SQL ); // execute an SQL statement without a result set using exec (); if ($ EXEC = 0 or $ EXEC = FALSE) {return FALSE;} else {return $ EXEC; // deletion successful. the number of affected items is returned.} 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 command (modify or delete). If no row is affected, 0 is returned.

Therefore, when you use PDO: exec () to execute the query command, because no row is modified or deleted, only 0 is returned.

$ QSQL = 'select '. $ strQTitle. 'from '. $ strTable. 'Where '. $ strQTitle. '= '. $ Value; $ QueryExec = $ PDO-> exec ($ QSQL); var_dump ($ QueryExec); if ($ QueryExec = 0 or $ QueryExec = FALSE) {die ('
The '. _ CLASS _.': '. _ FUNCTION _.' CLASS prompts an error: the data to be deleted does not exist. ');}


Sorry, I am confused. The previous two sections of code are all about this problem, that is, the exec () method is used. The first code is exec (). The query is correct, but the subsequent exec (insert) returns FALSE. if exec (query) is removed, exec (insert) it is successfully executed. What is the situation...
At the same time, in the second code, exec (query) Returns int (0), but actually int (1) is returned. Because the data exists, can it be deleted if it exists? Therefore, there are two problems, which are probably caused by one problem. The SQL statement does not seem to be any problem.

The SQL statement of the first code:
select ID,ENName from mh_forumpage where ENName="egfdfa"

This is the SQL statement executed by exec () when data is queried before data is inserted. Both the database and field names are correct.

In fact, if you delete the query validation before the delete and add data operation, it can also meet the normal function, but you do not understand why it is like this. Previously, mysqli was used, and PDO was just used.

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 command (modify or delete). If no row is affected, 0 is returned.

Therefore, when you use PDO: exec () to execute the query command, because no row is modified or deleted, only 0 is returned.



At this point, it is correct that exec () of the first code returns 0, because it is queried and verified before insertion, and 0 is returned correctly. However, the subsequent exec (insert) returns FALSE. if the previous query verification is removed, the subsequent exec (insert) is easy to use. I don't understand ..

The second code is a delete operation. query and verification is performed before deletion. exec () is used, but the return value is 0. if the data exists, 1 is returned.

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 command (modify or delete). If no row is affected, 0 is returned.

Therefore, when you use PDO: exec () to execute the query command, because no row is modified or deleted, only 0 is returned.



Oh .. That is to say, when the select statement is executed, whether the query is successful or not, are all 0 returned? In this case, the query without a result set cannot use exec.

Yes!
The select command cannot be executed using exec.

If false is returned, the command is incorrect.
You can view the results returned by the errorinfo method to find out what the problem is.

Yes!
The select command cannot be executed using exec.

If false is returned, the command is incorrect.
You can view the results returned by the errorinfo method to find out what the problem is.



Thank you for reducing the misunderstanding. the exec () method has always been a strange problem. Thank you very much.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.