今天做程序需要用到一个存储过程,然后用php程序调用。
存储过程如下:
delimiter $$
CREATE PROCEDURE disagree_upgrade_detail(a int,b text)
BEGIN
SET NAMES ‘utf8‘;
UPDATE `upgrade_detail` SET `disagree_description`=b WHERE `id`=a;
INSERT INTO `disagree_upgrade_detail` SELECT * from `upgrade_detail` WHERE `id`=a;
DELETE FROM `upgrade_detail` WHERE `id`=a;
END;
$$
PHP的调用代码片段如下:
mysql_query("CALL disagree_upgrade_detail({$which},‘{$why}‘);");
后来在跑程序的时候发现一个问题。
由于$why这个变量的值是大部分情况存在中文,在传递给存储过程后,变成了带有?的乱码。在此之后我检查了php、数据库的编码已经统一,搞了很久还是一筹莫展。
最后我在http://topic.csdn.net/u/20070703/09/fbceeb45-1c8d-4009-ac84-cabe9fc315d7.html中找到了答案。对其进行了如下修改
delimiter $$
CREATE PROCEDURE disagree_upgrade_detail(a int,b Nvarchar(4000))
BEGIN
SET NAMES ‘utf8‘;
UPDATE `upgrade_detail` SET `disagree_description`=b WHERE `id`=a;
INSERT INTO `disagree_upgrade_detail` SELECT * from `upgrade_detail` WHERE `id`=a;
DELETE FROM `upgrade_detail` WHERE `id`=a;
END;
$$
PHP的调用代码片段如下:
mysql_query("CALL disagree_upgrade_detail({$which},N‘{$why}‘);");
需要注意的代码已经标绿。在创建存储过程的时候,设计中文的参数的数据类型需为Nvarchar,而在传递参数的时候也需要多加一个N。