gpt4 book ai didi

php - php 查询结果为空

转载 作者:行者123 更新时间:2023-11-29 18:27:37 25 4
gpt4 key购买 nike

我有一个具有表“tb”的数据库

Column              Type---------------------------------id                  int(11)chainid             int(11)commission          decimal(10,2)order_count         int(11)total_order_value   decimal(10,2)fromdt              datetodt                date

I run this code from my file

$query = "DELETE FROM `tb` WHERE chainid=$row[0];";
error_log("1st : ".$query);

$storeit = mysql_query($query);
error_log("result of query : ".$storeit);

$query = "INSERT INTO `tb` VALUES(null,$row[0],$com,$oc,$ov,'$startdate','$enddate');";
error_log("2nd : ".$query);

$storeit = mysql_query($query);
error_log("result of query : ".$storeit);

但是数据库中没有任何反应,并且 error_log 中的打印是:

1st : DELETE FROM tb WHERE chainid=4 AND fromdt='2017-05-01' AND todt='2017-05-31';

result of query :

INSERT INTO tb VALUES(null,4,4755.69,94,6793.84,'2017-05-01','2017-05-31');

result of query :

当我使用这两句话时:

DELETE FROM `tb` WHERE chainid=4 AND fromdt='2017-05-01' AND todt='2017-05-31';
INSERT INTO `tb` VALUES(null,4,4755.69,94,6793.84,'2017-05-01','2017-05-31');

中的 SQL 选项卡中他们工作得很好!!

  • 所以,我不知道我的代码有什么问题?有什么我可以做的吗?

更新使用 http://php.net/manual/en/function.mysql-error.php 中的 [tag:mysql_error()] 后

function q($query){ 
$result = mysql_query($query);
if (mysql_errno()) {
$error = "MySQL error ".mysql_errno().": ".mysql_error()."\n<br>When executing:<br>\n$query\n<br>";
error_log($error);
}
}

并使用以下方法调用此函数:

$query = "DELETE FROM `tb` WHERE chainid=$row[0] AND fromdt='$startdate' AND todt='$enddate';";
$result = q($query);
$query = "INSERT INTO `tb` VALUES(null,$row[0],$com,$oc,$ov,'$startdate','$enddate');";
$result = q($query);

我得到了这些:

MySQL error 2014: Commands out of sync; you can't run this command now
When executing:
DELETE FROM tb WHERE chainid=4 AND fromdt='2017-05-01' AND todt='2017-05-31';

MySQL error 2014: Commands out of sync; you can't run this command now
When executing:
INSERT INTO tb VALUES(null,4,4755.69,94,6793.84,'2017-05-01','2017-05-31');

更新2

经过一番搜索:

Commands out of sync; you can't run this command now in Commands out of sync; you can't run this command now SQL

我发现我必须关闭连接并重新启动。但问题是我在 function.php 的开头使用一个函数来连接数据库,该函数包含在我的 header.php 文件中:

function db_connect()
{
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "dbname";


@mysql_connect($servername,$username,$password) or die ("error in host connection");
header('Content-Type: text/html; charset=utf-8');
mysql_set_charset('utf8_unicode_ci');
@mysql_select_db($dbname) or die("error in db connection");
mysql_query("SET NAMES 'utf8'");
}

db_connect();
  • 那么,如何使用 mysql_close(); 关闭连接?

最佳答案

我只需要在 mysql_query 使用之前关闭连接:

mysql_close();

并立即使用我的函数再次打开连接:

db_connect();

非常感谢所有为解决此问题做出贡献的人。

关于php - php 查询结果为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46029913/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com