Has anyone ever told you that your SQL statement has returned an error?

Source: Internet
Author: User
Tags sql error
Has anyone ever told you that your SQL statement has returned an error? Quote: The real environment for online operation is always unpredictable. it is clear that when you test locally, all the situations are OK, maybe you are still excited about the cool effects and amazing designs in some of your places! However, after going online, it will occasionally be the same as that of others. there will be problems with you, and there will be problems! I am wondering, how can a problem occur? Haha, this may be the most common sighs of most siege Lions!

So today, let's talk about how to find your errors and how to quickly locate them, instead of waiting for the user to come to you for feedback, because it may have been wrong for too long! What we need to do is to discover the error in the first place, solve the problem in the bud, and make a summary afterwards to avoid making similar mistakes in the future! The mistake is not terrible. What is terrible is that you have always made the same mistake. what is the difference between you and new users?

Notes:

1. This article mainly solves SQL-related errors;

2. This article is based on the PHP perspective. (mom, who told me that I am engaged in PHP Development ?)

3. the problems solved in this article are: 1. how to discover the problems; 2. how to solve the problems;

4. question and supplement;

Question 1: How to discover problems?

You don't need to test anything. who is willing to go online without testing? If that is the case, I can only say, you are awesome! Testing is a very important line of defense. In general, after testing, after the function is launched, there will not be too many bugs, or it will not be too obvious! Okay, let's assume we haven't found any problem in the test. let's go online! In fact, after the launch, we have no bottom in our hearts, especially when there is no perfect system for deployment, there will always be a variety of strange features before and after the launch. Developers and users are at two levels. what will happen to the ghost? Our Treasure is totally dependent on user operations and user feedback? Oh, no, that's too passive!

Actively discover problems. 1. after going online, you won't be able to perform another test. now that you are a common user, you must perform the test on your own, you can confirm the basic functions. OK. Next, we will give it to the user! 2. if you do not have a backend, you will be handed over to the user. you are still too Low, because necessary monitoring measures must be available! The monitoring here refers to the program-level, that is, the so-called error. How to record the error message? How can I know the error? I am engaged in PHP development. PHP has an error logging function, error_reporting. open this function and specify the location and level of the error log to record PHP errors. However, you must disable the error page display, otherwise, you will be finished when you see such errors! Since this article is about SQL errors, it should not be here. But what I want to say is that when an SQL error is reported, PHP has also reported an error, which is usually a warning-level error and is often associated, the following statements depend on the above query, and the above error has been reported, so the subsequent error will also be reported. PHP knows the error, but it's just a rough idea. where is the SQL statement wrong? Only he knows it. just give it to him! The general principle is to record error logs when an error occurs in the query. the error logs mainly record the following information: error information, file location (trace each file to its origin until it is accessed ), the following is a sample PHP code that records error information for your reference:

 _ Logfile) {if (! $ Msg) {if (! Mysql_errno () {return;} $ msg = "mysql_errno :". mysql_errno (). "\ nmysql_error :". mysql_error ();} file_put_contents ($ this-> _ logfile ,'['. date ('Y-m-d H: I: s '). "] $ msg \ n", FILE_APPEND); $ trace = debug_backtrace (); foreach ($ trace as $ call) {if (empty ($ call ['file']) & amp; empty ($ call ['line']) {continue;} file_put_contents ($ this-> _ logfile, "{$ call ['file']} on line {$ call ['line']} \ n", FILE_APPEND);} file_put_contents ($ this-> _ logfile, "\ n", FILE_APPEND );}}

In this way, you have something for your reference. when you change it, you only need to check the instructions in this file and you will probably know what is wrong, everything is wrong. I think it's only a matter of time to solve the problem, and it's a matter of short time!

Question 2: How to solve the problem?

As I have already said before, since the root cause of the problem has been found, it is only a short time to solve the problem. However, I will still provide some solutions for reference. after all, everyone is so busy. How long can I crack your bad code! The following are common errors:

1. Error: [2002] Unable to connect due to the active rejection of the target computer. Resolution: The database connection information is incorrect. you may have deployed the connection to the test environment online. this is a big problem. Can you recover it quickly? Note: If no error is reported, it is because your online machine can connect to the test database;

2. Error: [1146] Table 'bbbq' doesn' t exist. Resolution: The table does not exist. check whether the newly added table is not added to the production environment or the table name is incorrect? Note: If you have passed the test, it is unlikely that the table name is incorrect!

3. Error: [1054] Unknown column 'column _ X' in 'Field list '. Resolution: The specified column does not exist. either it does not exist in the database or it is written incorrectly. check the column quickly! Note: Same as above.

4. Error: [1366] Incorrect string value: '\ xA9 \ x96' for column 'X _ name' at row 1. Resolution: a character set problem occurs. if a gbk word is set to gb2312 for receiving, this problem occurs. please try again! Note: use UTF-8 encoding whenever possible. it is convenient to code and store data in the database!

5. Error: [1364] Field 'pid 'doesn' t have a default value. Resolution: The specified field does not have a default value. check whether the obtained value is not obtained and the value is null. specify the database default value for some unnecessary fields. Note: If there is an index relationship, please be sure to set the not null option, otherwise the index may become invalid!

6. Error: [1366] Incorrect integer value: ''for column 'townid' at row 1. Resolution: the given value does not meet the field type requirements. before receiving the database, confirm the type of the field, perform mandatory conversion, and then import the database. Note: In some versions of mysql, you can forcibly convert the type to solve this problem, but the result may have exceeded your expectation!

...

Wait, you have to raise your family and rush to work! Keyword: load data infile, REPLACE...

Related Article

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.