Debugging methods to be mastered when using ThinkPHP
I often see people asking questions about the types of findAll returned data and the reasons for errors. I am still not familiar with the built-in debugging methods and methods of ThinkPHP, aside from the debugging methods provided by IDE, if you are using or planning to use ThinkPHP for development, you should understand and master the following methods related to debugging:
1. Open the debug mode DEBUG_MODE in the project configuration file, so that you can find most of the error causes. The output of the Verification Code may be affected.
2. If you do not want to use debug mode, you can enable page Trace display separately. The reason why many people do not want to use the debug mode is that there is output of page Trace information. In fact, there is a misunderstanding in this case that there must be page Trace in the debug mode, however, the debugging mode does not necessarily have to do with page Trace, because after the debugging mode is enabled, the system's default debugging configuration file will enable page Trace display, therefore, you can define the debugging configuration file for the project separately.
3. Using the system-defined dump function, this method is the same as var_dump, which can output any type of variable information, and is more conducive to viewing in the browser, for example:
Copy codeThe Code is as follows:
$ User = D ("User ");
$ List = $ User-> findAll ();
Dump ($ list );
4. The page Trace information can only display the SQL statements executed on the current page, but cannot view the SQL statements in the background operations executed by ajax, therefore, you can enable the SQL log record SQL _DEBUG_LOG to record each SQL statement executed, and view the execution time of each SQL statement. The SQL log file is located under the Logs directory, the daily SQL logs are automatically distinguished by date.
5. If you suspect that SQL Execution is incorrect after performing a data operation, you can use the model classGetLastSqlMethod to view the last executed SQL statement to analyze the specific cause of the error. For example:
Copy codeThe Code is as follows:
$ User = D ("User ");
$ User-> id = 3;
$ User-> name = 'thinkphp ';
$ User-> save ();
Echo $ User-> getLastSql ();
// Output update think_user set name = 'thinkphp' where id = 3;
6. When debugging the running time of a code segment, you can use the debug_start ($ label) and debug_end ($ label) methods provided by the system. For example:
Copy codeThe Code is as follows:
Debug_start ('Demo ');
// Here is your code snippet .......
Debug_end ('Demo ');