After talking about the connection, we will talk about the query.
The following two interfaces are used for MySQL execution and query:
1. mysql_query (MySQL * MySQL, const char * SQL );
2. Int mysql_real_query (MySQL * MySQL, const char * query, unsigned long
Length );
The two functions are described as follows:
1.Mysql_query ()
Int mysql_query (MySQL * MySQL, const char * query)
Description
Execute the SQL query pointed to by the "null-terminated string" query. Normally, a string must contain one SQL statement,The end semicolon (';') or "/g" should not be added for the statement ". If multi-statement execution is allowedA string can contain multiple statements separated by semicolons. However, the client_multi_statements option must be specified during connection.
Mysql_query () cannot be used for queries that contain binary data. Replace mysql_real_query () with mysql_real_query () (binary data may contain characters '/0', mysql_query () this character is interpreted as the end of the query string ).
If you want to know whether the query should return a result set, you can use mysql_field_count () to check. See section 25.2.3.22, "mysql_field_count ()".
Return Value
If the query is successful, 0 is returned. If an error occurs, a non-zero value is returned.
2.Mysql_real_query ()
Int mysql_real_query (MySQL * MySQL, const char * query, unsigned long
Length)
Description
Execute the SQL query directed by "query", which should be a String Length Byte "long ". Normally, a string must contain one SQL statement,The end semicolon (';') or "/g" should not be added to the statement". If multiple statements can be executed, a string can contain multiple statements separated by semicolons. However, the client_multi_statements option must be specified during connection.
For queries that contain binary data, you must use mysql_real_query () instead of mysql_query (). This is because binary data may contain '/0' characters. In addition, mysql_real_query () is faster than mysql_query () because it does not call strlen () on the query string ().
If you want to know whether the query should return the result set, you can use mysql_field_count () to check section 25.2.3.22, "mysql_field_count ()".
Return Value
If the query is successful, 0 is returned. If an error occurs, a non-zero value is returned.
If it fails, use mysql_error (MySQL * MySQL) to check the error message.
Use mysql_query and mysql_real_query to execute any MySQL statements. You do not need to add a semicolon at the end of the statement!
For queries that do not have the same query as select, you need to call mysql_store_result or mysql_use_result to save the result set.
For insert, delete, and create statements that do not return a result set, determine whether the returned value is successful and use the mysql_affected_rows function.
Query the number of affected rows. Use mysql_error (MySQL * MySQL) to view the error message.