php调用mysql存储过程(关于CLIENT_MULTI_RESULTS)

以为php有用于调用存储过程的内建函数,查了一下发现只能用mysql_query(callpro())这样的方式,我认为从本质上也就相当于在mysql命令行里执行语句了,由于我的存储过程含有输入输出参数,直接调用会报一个mysql_error错误:XXXXcan'treturn a result set in the given context

google了一下这个错误发现有人用以下的代码解决了这个问题:
原文地址:http://www.phpweblog.net/GaRY/archive/2008/01/29/2752.html#Post

关键就是两点

1 define ( ' CLIENT_MULTI_RESULTS ' ,   131072 );
2 
3  $link   =   mysql_connect ( " 127.0.0.1 " ,   " root " ,   "" , 1 , CLIENT_MULTI_RESULTS) or  die ( " Could not connect:  " . mysql_error ());

下面就可以正常使用了,以下是例子程序。

 

 1  <? php
 2       define ( ' CLIENT_MULTI_RESULTS ' ,   131072 );
 3 
 4       $link   =   mysql_connect ( " 127.0.0.1 " ,   " root " ,   "" , 1 , CLIENT_MULTI_RESULTS) or  die ( " Could not connect:  " . mysql_error ());
 5       mysql_select_db ( " vs " ) or  die ( " Could not select database " );
 6  ?>
 7 
 8 <? php
 9           $result   =   mysql_query ( " call get_news_from_class_id(2) " ) or  die ( " Query failed: "   . mysql_error ());
10           while ( $row   =   mysql_fetch_array ( $result ,  MYSQL_ASSOC))
11          {
12                   $line   =   ' <tr><td><a target = _blank href=/ '' .$row["url"]. ' / ' > ' . $row [ " title " ] . ' ( ' . $row [ " page_time " ] . ' ) ' . ' </a></td></t r> ' ;
14                   echo   $line ;
15                   printf ( " /n " );
16 
17          }
18           mysql_free_result ( $result );
19 ?>
20 
21  <? php
22       mysql_close ( $link );
23  ?>


   其中的一个参数CLIENT_MULTI_RESULTS不明白是什么意思,google之,在mysql的官方主页上关于mysql提供的c接口的文档(http://dev.mysql.com/doc/refman/5.0/en/mysql-real-connect.html)里找到了这个参数和其他一些参数,我大概翻译了一下描述,如下:
Flag NameFlagDescription
CLIENT_COMPRESSUse compression protocol.(使用压缩协议。)
CLIENT_FOUND_ROWSReturn the number of found (matched) rows, not the number ofchanged rows.(返回找到(匹配)的行数,而不是改变了的行数。)
CLIENT_IGNORE_SIGPIPEPrevents the client library from installing a SIGPIPE signal handler. This can be used to avoidconflicts with a handler that the application has alreadyinstalled.(阻止客户端库安装一个SIGPIPE信号处理器。这个可以用于当应用程序已经安装该处理器的时候避免与其发生冲突。
CLIENT_IGNORE_SPACEAllow spaces after function names. Makes all functions namesreserved words.(允许在函数名后使用空格。所有函数名可以预留字。)
CLIENT_INTERACTIVEAllow interactive_timeout seconds(instead of wait_timeout seconds) ofinactivity before closing the connection. The client's sessionwait_timeout variable is set tothe value of the session interactive_timeoutvariable.(允许使用关闭连接之前的不活动交互超时的描述,而不是等待超时秒数。客户端的会话等待超时变量变为交互超时变量。)
CLIENT_LOCAL_FILESEnable LOAD DATA LOCAL handling.
CLIENT_MULTI_RESULTSTell the server that the client can handle multiple result setsfrom multiple-statement executions or stored procedures. This flagis automatically enabled if CLIENT_MULTI_STATEMENTS is enabled. See the notefollowing this table for more information about thisflag.(通知服务器客户端可以处理由多语句或者存储过程执行生成的多结果集。当打开CLIENT_MULTI_STATEMENTS时,这个标志自动的被打开。可以在本表后查看更多关于该标志位的信息。
CLIENT_MULTI_STATEMENTSTell the server that the client may send multiple statements ina single string (separated by “;”). If this flag is not set,multiple-statement execution is disabled. See the note followingthis table for more information about this flag.(通知服务器客户端可以发送多条语句(由分号分隔)。如果该标志为没有被设置,多条语句执行。)
CLIENT_NO_SCHEMADon't allow the db_name.tbl_name.col_name syntax.This is for ODBC. It causes the parser to generate an error if youuse that syntax, which is useful for trapping bugs in some ODBCprograms.(不允许“数据库名.表名.列名”这样的语法。这是对于ODBC的设置。当使用这样的语法时解析器会产生一个错误,这对于一些ODBC的程序限制bug来说是有用的。
CLIENT_ODBCUnused.(不使用)
CLIENT_SSLUse SSL (encrypted protocol). This option should not be set byapplication programs; it is set internally in the client library.Instead, use mysql_ssl_set() before calling mysql_real_connect().(使用SSL。这个设置不应该被应用程序设置,他应该是在客户端库内部是设置的。可以在调用mysql_real_connect()之前调用mysql_ssl_set()来代替设置。
CLIENT_REMEMBER_OPTIONSRemember options specified by calls to mysql_options(). Without this option, ifmysql_real_connect() fails, you must repeatthe mysql_options() calls before trying to connectagain. With this option, the mysql_options() calls need not berepeated.(记住通过调用mysql_options()生成的设置。如果不使用这个设置,当mysql_real_connect失败时,再重新连接之前必须反复调用mysql_options()。当然,如果使用这个设置,就不必反复调用了。

   下面有对于 CLIENT_MULTI_STATEMENTS的说明:
If you enable CLIENT_MULTI_STATEMENTSor CLIENT_MULTI_RESULTS, you shouldprocess the result for every call to mysql_query() or mysql_real_query() by using a loop that calls mysql_next_result() to determine whether there are moreresults. For an example, see Section 20.9.12, “C API Support for MultipleStatement Execution”.
如果打开了 CLIENT_MULTI_STATEMENTS或 CLIENT_MULTI_RESULTS,你必须对每一个mysql_query()或者mysql_real_query()的调用结果通过一个循环来处理,在这个循环中,调用mysql_next_result()来决定(发现)是否有更多的结果,如 Section 20.9.12,“C API Support for Multiple Statement Execution”

   以上供需要的朋友参考吧。
转自:http://blog.sina.com.cn/s/blog_550ffb0b0100dybn.html
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值