0

I have a 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 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 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 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');

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(); ?
Hossam
  • 177
  • 1
  • 11
  • `Delete` and `insert` dont return anything. Are the rows gone/inserted? You also shouldn't use `mysql_` functions anymore, use `mysqli` or `pdo` for future developments. – chris85 Sep 04 '17 at 03:32
  • nothing deleted and nothing inserted at all – Hossam Sep 04 '17 at 03:33
  • 2
    Look for errors, http://php.net/manual/en/function.mysql-error.php. – chris85 Sep 04 '17 at 03:34
  • 3
    Yeah don't use mysql * functions – Rotimi Sep 04 '17 at 03:36
  • after using [tag:mysql_error] I got these: 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';
    **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
  • 1
    Add that to the question please, https://stackoverflow.com/posts/46029913/edit. – chris85 Sep 04 '17 at 03:52

1 Answers1

0

I just needed to close connection before mysql_query using:

mysql_close();

and open a connection again immediately using my function:

db_connect();

Many thanks to everyone who contributed trying to solve this problem.

Hossam
  • 177
  • 1
  • 11