MYSQL client program design errors to be avoided

Source: Internet
Author: User
This article discusses some common MySQLCAPI program design errors and how to avoid them (these problems will pop up periodically in the MySQL mail list ). 1. Error 1 -- using the uninitialized connection handler pointer we have passed the NULL parameter to call mysql_init (), which is to let it allocate and initialize MYSQL

This article discusses some common MySQLC API program design errors and how to avoid them (these problems will pop up periodically in the MySQL mail list ). 1. Error 1 -- use the uninitialized connection handler. We have passed the NULL parameter to call mysql _ I n I t (). This is to let it allocate and initialize the MYSQL knot.

This article discusses some common MySQLC API program design errors and how to avoid them (these problems will pop up periodically in the MySQL mail list ).

1. Error 1 -- use uninitialized connection HandlerNeedle

We have passed the NULL parameter to call mysql _ I n I t (). This is to let it allocate and initialize the MYSQL structure, and then return a pointer. Another method is to pass the pointer to an existing MYSQL structure. In this case, mysql_init () initializes the structure and returns a pointer without having to allocate the structure itself. If you want to use the second method, be careful when there are some subtle problems. The following discussion points out some issues that need attention. If you pass a pointer to mysql _ init (), it should actually point to something. See the following code snippet:
498) this. width = 498; 'onmousewheel = 'javascript: return big (this) 'height = 114 src = "/files/uploadimg/20051129/1650040 .JPG" width = 214>
This problem is that mysql_init () gets a pointer, but the pointer does not point anywhere you know. Conn is a local variable. Therefore, when main () starts execution, it is an uninitialized memory that can point to any place. This means mysql_init () will use a pointer, you can overwrite data in any area of the memory. If you are lucky, the conn points to the external address space of your program, so that the system will immediately terminate, so that you can be aware of problems in the Code as soon as possible.

If unfortunately, the conn points to the internal data that will not be used in the program until the data is used again. Therefore, the actual problems are far more difficult to capture than the problems encountered during program execution. The following is a piece of problematic code:
498) this. width = 498; 'onmousewheel = 'javascript: return big (this) 'height = 172 src = "/files/uploadimg/20051129/1650041 .JPG" width = 345>
Conn is a global variable. Therefore, before the program starts, it is initialized to 0 (that is, NULL ). Mysql_init () encounters a NULL parameter, so a new connection handler is initialized and assigned. The system crashes as long as the conn is passed to the MySQLCAPI function that requires a non-NULL connection handler. The modification of these code segments is to ensure that conn has a known value. For example, you can initialize it to the allocated MYSQL schema address:
498) this. width = 498; 'onmousewheel = 'javascript: return big (this) 'height = 59 src = "/files/uploadimg/20051129/1650042 .JPG" width = 337>
However, recommended (easier !) The solution is to explicitly pass NULL to mysql _ init (), assign the MYSQL structure to the function, and assign the return value to conn:
498) this. width = 498; 'onmousewheel = 'javascript: return big (this) 'height = 62 src = "/files/uploadimg/20051129/1650043 .JPG" width = 221>
In any case, do not forget to check the return value of mysql_init () to ensure that it is not NULL.

2. Error 2 -- An error occurred while verifying the valid result set.

Remember to check the call status of the expected result set. The following code does not do this:
498) this. width = 498; 'onmousewheel = 'javascript: return big (this) 'height = 153 src = "/files/uploadimg/20051129/1650044 .JPG" width = 410>
Unfortunately, if mysql_store_result () fails, res_set is NULL, And the while loop is not executed, the return value of the result set function should be tested to ensure that the operation is actually in progress.

3. Error 3 -- Failure Caused by NULL column Value

Do not forget to check whether the column value in MYSQL_ROW returned by mysql_fetch_row () is a NULL pointer. If row [I] is NULL, the following code will crash on some machines:
498) this. width = 498; 'onmousewheel = 'javascript: return big (this) 'height = 140 src = "/files/uploadimg/20051129/1650045 .JPG" width = 393>
The most harmful part of this error is that some printf () versions are tolerant of outputting "(NULL)" to the null pointer )", this makes it easy to escape errors without locating them. If you give the program to a friend who is not very tolerant of the printf () version, the program will crash and your friends will think that you are a useless programmer. The loop should be written as follows:
498) this. width = 498; 'onmousewheel = 'javascript: return big (this) 'height = 145 src = "/files/uploadimg/20051129/1650046 .JPG" width = 427>
The only time that you do NOT need to check whether the column value is null is when the column information structure determines that IS _ NOT _ NULL () IS true.

4. Error 4 -- passing meaningless result Buffer

The client library function that requires you to provide a buffer usually needs to make these buffers actually exist. The following Code violates this rule:
498) this. width = 498; 'onmousewheel = 'javascript: return big (this) 'height = 100 src = "/files/uploadimg/20051129/1650047 .JPG" width = 512>
What is the problem? To_str must point to an existing buffer, but it does not exist in this example. Therefore, it points to a random location. Do not pass meaningless pointer to mysql_escape_string as the to_str parameter; otherwise, it will trample on the memory.

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.