为多个查询保持 MYSQL 连接功能
keep MYSQL connection functional for multiple queries
我很难找到关于这个主题的信息。我在 Raspberry Pi 上有一个 运行 应用程序,其中我有一个带有以下代码的无限循环。在它之外,我有 MYSQL *con;
可以重复使用。所以我的代码第一次运行良好,但第二次出现以下错误。我认为添加 MYSQL_close()
可以解决问题,但事实并非如此。
输出:
valor: d9274cb5 -651735883
valor: d9274cb5 -651735883
1
This handle is already connected. Use a separate handle for each connection.
代码:
uint32_t intVal;
sscanf(&sn_str[1],"%x",&intVal);
fprintf(stderr, "valor: %x %d\n", intVal, intVal);
if (mysql_real_connect(con, "localhost", "rfid", "******",
"Order2Dock", 0, NULL, 0) == NULL)
{
fprintf(stderr, "1 \n");
finish_with_error(con);
}
if (mysql_query(con, "INSERT INTO `Order2Dock`.`Actividad`(`TiempoInicio`,`Proceso_Id`, `Orden_Id`) VALUES (now(),1,1)")) {
fprintf(stderr, "2 \n");
finish_with_error(con);
}
mysql_close(con);
听说 "mysql" 会在以后 php 删除..
使用 "mysqli" 你可以使用 mysqli_multi_query
将连接命令放在循环之外
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
if (!$mysqli) {
fprintf(stderr, "1 \n");
finish_with_error(con);
}
然后开始循环,不需要每次循环都连接:
while (1+1 = 2){
if (!$mysqli){
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
}
uint32_t intVal;
sscanf(&sn_str[1],"%x",&intVal);
fprintf(stderr, "valor: %x %d\n", intVal, intVal);
if ($mysqli->query("INSERT INTO `Order2Dock`.`Actividad`(`TiempoInicio`,`Proceso_Id`, `Orden_Id`) VALUES (now(),1,1)") === TRUE) {
echo '<p>Yeah, another query!!</p>';
}
}
编辑:我刚刚添加了一个条件来测试 link 是否仍然可用,否则重新连接到数据库。
我只是在想,如果这是一个无限循环 运行 在像 apache 或 IIS 这样的 Web 服务器上...那么必须配置一些东西才能让脚本 运行 永远而是防止网络服务器超时。
干杯。
我很难找到关于这个主题的信息。我在 Raspberry Pi 上有一个 运行 应用程序,其中我有一个带有以下代码的无限循环。在它之外,我有 MYSQL *con;
可以重复使用。所以我的代码第一次运行良好,但第二次出现以下错误。我认为添加 MYSQL_close()
可以解决问题,但事实并非如此。
输出:
valor: d9274cb5 -651735883
valor: d9274cb5 -651735883
1
This handle is already connected. Use a separate handle for each connection.
代码:
uint32_t intVal;
sscanf(&sn_str[1],"%x",&intVal);
fprintf(stderr, "valor: %x %d\n", intVal, intVal);
if (mysql_real_connect(con, "localhost", "rfid", "******",
"Order2Dock", 0, NULL, 0) == NULL)
{
fprintf(stderr, "1 \n");
finish_with_error(con);
}
if (mysql_query(con, "INSERT INTO `Order2Dock`.`Actividad`(`TiempoInicio`,`Proceso_Id`, `Orden_Id`) VALUES (now(),1,1)")) {
fprintf(stderr, "2 \n");
finish_with_error(con);
}
mysql_close(con);
听说 "mysql" 会在以后 php 删除.. 使用 "mysqli" 你可以使用 mysqli_multi_query
将连接命令放在循环之外
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
if (!$mysqli) {
fprintf(stderr, "1 \n");
finish_with_error(con);
}
然后开始循环,不需要每次循环都连接:
while (1+1 = 2){
if (!$mysqli){
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
}
uint32_t intVal;
sscanf(&sn_str[1],"%x",&intVal);
fprintf(stderr, "valor: %x %d\n", intVal, intVal);
if ($mysqli->query("INSERT INTO `Order2Dock`.`Actividad`(`TiempoInicio`,`Proceso_Id`, `Orden_Id`) VALUES (now(),1,1)") === TRUE) {
echo '<p>Yeah, another query!!</p>';
}
}
编辑:我刚刚添加了一个条件来测试 link 是否仍然可用,否则重新连接到数据库。
我只是在想,如果这是一个无限循环 运行 在像 apache 或 IIS 这样的 Web 服务器上...那么必须配置一些东西才能让脚本 运行 永远而是防止网络服务器超时。
干杯。