作者:吴炳锡 来源:http://www.mysqlsupport.cn/ 联系方式: wubingxi#gmail.com 转载请注明作/译者和出处,并且不能用于商业用途,违者必究.
今天遇一个同学聊存储过程返回值经常得到意外的值为null, 因为白天有事,晚上给做一个实验放在这里供有相应问题的同学查看一下。
存储过程:
delimiter //
create procedure usp_s2(out par1 int)
begin
select inet_ntoa(ip) , port from proxy_list limit 5;
select count(*) into par1 from proxy_list;
END//
delimiter ;
session 1执行:
mysql> call usp_s2(@a);
+---------------+------+
| inet_ntoa(ip) | port |
+---------------+------+
| 1.34.21.86 | 8088 |
| 1.34.59.50 | 8088 |
| 1.34.69.15 | 8088 |
| 1.34.73.110 | 8088 |
| 1.34.76.218 | 8088 |
+---------------+------+
5 rows in set (0.00 sec)
Query OK, 1 row affected (0.01 sec)
mysql> select @a;
+------+
| @a |
+------+
| 4430 |
+------+
1 row in set (0.00 sec)
session 2执行:
mysql> select @a;
+------+
| @a |
+------+
| NULL |
+------+
1 row in set (0.00 sec)
可见两个session得到的结果不一致。 基本可以肯定两次调用落入不同的会话中会得到不同的值。
为了一致可以如用如下调用:
multi_query("call usp_s2(@total); select @total;");
if ($result) {
do {
if ($r = $db->store_result()) {
if ( $r->field_count == 2){
while( $row = $r->fetch_row() ){
print "ip: $row[0], port: $row[1]\n";
}
}else{
$row = $r->fetch_row();
print "total: $row[0]\n";
}
}
} while ( $db->next_result() );
}
$db->close();
?>
$php t_proc_return.php
ip: 1.34.21.86, port: 8088
ip: 1.34.59.50, port: 8088
ip: 1.34.69.15, port: 8088
ip: 1.34.73.110, port: 8088
ip: 1.34.76.218, port: 8088
total: 4430
Good luck.