I have a mysql database with a table "tb"
Column Type --------------------------------- id int(11) chainid int(11) commission decimal(10,2) order_count int(11) total_order_value decimal(10,2) fromdt date todt date
I run this code from my php 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);
but there's nothing happens in DB, and the prints in error_log was:
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 :
while when I use these two sentences:
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');
in SQL tab in phpmyadmin they worked perfectly!!
- So, I don't know what is wrong with my code? is there something I can do?
UPDATE after using [tag:mysql_error()] from http://php.net/manual/en/function.mysql-error.php
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);
}
}
and called this function instead, using:
$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);
I got these:
MySQL error 2014: Commands out of sync; you can't run this command now
When executing:
DELETE FROMtb
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 INTOtb
VALUES(null,4,4755.69,94,6793.84,'2017-05-01','2017-05-31');
UPDATE 2
after a little search about:
Commands out of sync; you can't run this command now in Commands out of sync; you can't run this command now SQL
I figured out that I have to close connection and start it again. but the problem is I'm using a function to connect database in the beginning of my function.php which included in my header.php file :
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();
- So, how to close the connection using mysql_close(); ?
When executing:
DELETE FROM `tb` WHERE chainid=4 AND fromdt='2017-05-01' AND todt='2017-05-31';
**AND** 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');
– Hossam Sep 04 '17 at 03:45