Home > Database > Mysql Tutorial > MySQL 一次执行多条语句的实现及常见问题

MySQL 一次执行多条语句的实现及常见问题

WBOY
Release: 2016-06-07 18:01:34
Original
1728 people have browsed it

通常情况MySQL出于安全考虑不允许一次执行多条语句(但也不报错,很让人郁闷)。

MySQL是支持在单个查询字符串中指定多语句执行的,使用方法是给链接指定参数:
代码如下:
//链接时设定
mysql_real_connect( ..., CLIENT_MULTI_STATEMENTS );
//或者
//中途指定
mysql_set_server_option( mysql, MYSQL_OPTION_MULTI_STATEMENTS_ON ); //mysql是连接的名称

当使用执行多语句功能后,一定要读完整个resault集,否则会出现错误:Commands out of sync; you can't run this command now
官方推荐的执行语句是这样的:
代码如下:
do
{
/* Process all results */
...
printf( "total affected rows: %lld", mysql_affected_rows( mysql ) );
...
if( !( result mysql_store_result( mysql ) ) )
{
printf( stderr, "Got fatal error processing query\n" );
exit(1);
}
process_result_set(result); /* client function */
mysql_free_result(result);
}while( !mysql_next_result( mysql ) );

如果仅仅是插入等不需要返回值的SQL语句,也一样得读完整个resault集并释放,最小化的写法:
代码如下:
do
{
result = mysql_store_result( mysql );
mysql_free_result(result);
}while( !mysql_next_result( mysql ) );
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template