mysql 不同步存储过程,mysql存储过程错误命令不同步;您现在不能运行此命令

I have a stored procedure which I call from PHP which works fine on my dev version which is running Maria DB. I transferred to production which is running mysql (5.5.53) a 'Commands out of sync; you can't run this command now' error. I have tried suggestions to make sure the connection is closed after each query. I have also tried $stmt->store_result(); & $stmt->free_result();. If I try $stmt-next_result(); I do not get this out of sync error, but my script exists the loop and I do not get any other results.

Stored Procedure:

CREATE DEFINER=`root`@`localhost` PROCEDURE `test3`(IN `Input_Value` INT, OUT `Out_val` VARCHAR(150))

BEGIN

SET @sql = 'SELECT sample_name FROM sample';

PREPARE stmt FROM @sql;

EXECUTE stmt;

END

PHP:

`foreach($locations as $location_name){

echo $location_name.'
';

$sp_query = "CALL test3(?,@Out_val)";

$stmt = $dbc->prepare($sp_query);

if(!$stmt){

echo mysqli_error($dbc);

}

$stmt->bind_param('i',$view);

if ($stmt->execute()){

$params = array();

$meta = $stmt->result_metadata();

print_r($meta);

while ($field = $meta->fetch_field()){

$params[] = &$row[$field->name];

}

print_r($params);

call_user_func_array(array($stmt, 'bind_result'), $params);

$header_ct = 0;

$labels = '';

while ($stmt->fetch()) {

foreach($row as $key => $value){

$key = htmlspecialchars($key);

//echo "Key:".$key."
";

//echo "Value:".$value."
";

}

}

}else{

echo mysqli_error($dbc);

echo "not working";

}

$stmt->store_result();

//$stmt->free_result();

$stmt->close();

}

?>`

解决方案

In Just seperate your queries with $select_stmt->close(); to split them (not simultaneous but procedural.

For more on this see

If you are using codeigniter Framework,

add following code into /system/database/drivers/mysqli/mysqli_result.php

For codeigniter

function next_result()

{

if (is_object($this->conn_id))

{

return mysqli_next_result($this->conn_id);

}

}

then in model when you call Stored Procedure

$query = $this->db->query("CALL test()");

$res = $query->result();

//add this two line

$query->next_result();

$query->free_result();

//end of new code

return $res;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MySQL主从同步有两种方式:基于语句的复制和基于行的复制。无论使用哪种方式,都需要进行以下几个步骤: 1. 在主服务器上开启二进制日志功能 2. 在主服务器上创建一个用于从服务器连接的用户,并赋予该用户复制权限 3. 在从服务器上配置主服务器的相关信息,并启动从服务器的复制进程 以下是基于语句的复制的具体命令行操作: 1. 在主服务器上开启二进制日志功能 ``` vim /etc/my.cnf ``` 在 [mysqld] 段中添加如下配置: ``` log-bin=mysql-bin binlog-format=statement ``` 保存并退出,重启 MySQL 服务使配置生效。 2. 在主服务器上创建一个用于从服务器连接的用户,并赋予该用户复制权限 ``` CREATE USER 'replica'@'%' IDENTIFIED BY 'password'; GRANT REPLICATION SLAVE ON *.* TO 'replica'@'%'; FLUSH PRIVILEGES; ``` 注意:在 MySQL 8.0 版本及以上,需要将 REPLICATION SLAVE 改为 REPLICATION SLAVE PROXY。 3. 在从服务器上配置主服务器的相关信息,并启动从服务器的复制进程 ``` vim /etc/my.cnf ``` 在 [mysqld] 段中添加如下配置: ``` server-id=2 relay-log=mysql-relay-bin log_slave_updates=1 read_only=1 ``` 其中,server-id 是从服务器的唯一标识符,可以设置为任意整数;relay-log 是从服务器用于存储复制事件的日志文件;log_slave_updates=1 表示从服务器将接收到的变更事件也写入自己的二进制日志文件中;read_only=1 表示从服务器只能读取数据,不能写入数据。 保存并退出,重启 MySQL 服务使配置生效后,执行以下命令启动从服务器的复制进程: ``` CHANGE MASTER TO MASTER_HOST='master_ip', MASTER_USER='replica', MASTER_PASSWORD='password', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=100; START SLAVE; ``` 其中,MASTER_HOST 是主服务器的 IP 地址;MASTER_USER 和 MASTER_PASSWORD 是在主服务器上创建的用于从服务器连接的用户和密码;MASTER_LOG_FILE 和 MASTER_LOG_POS 分别是主服务器当前二进制日志文件的名称和位置,可以通过在主服务器上执行 SHOW MASTER STATUS 命令获取。 至此,基于语句的复制的主从同步已经完成。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值