-1
$query = mysql_query(
    "SELECT SUM( usd_amount ) AS value_sum 
     FROM order_tbl 
     WHERE o_date BETWEEN SUBDATE( CURDATE( ) , 
          DAYOFMONTH( CURDATE( ) ) -1 ) AND CURDATE()
");

if(!$query){
        echo "Did Not Execute the query";
        echo mysqli_error();

}

I keep getting the "Did Not Execute the query" but the query runs fine in phpMyAdmin. The echo for mysqli_error() is also not displaying anything.

Also, I have checked the connection and the connection works absolutely correct.

Francisco
  • 10,918
  • 6
  • 34
  • 45
  • Perhaps using [mysql_error()](http://www.php.net/manual/en/function.mysql-query.php) to find out why it didn't execute the query might help – Mark Baker Mar 05 '16 at 19:02
  • 2
    `mysql_query()` is depricated. Always try to use `mysqli_query()` . Avoid using mysql API. Use mysqli API. – Mathews Mathai Mar 05 '16 at 19:03
  • 2
    don't use `mysql_*`, it's deprecated now. use `mysqli_*` or `PDO`. – Alive to die - Anant Mar 05 '16 at 19:03
  • @Anant: Connection object is optional. Real problem is query is [syntactically invalid](http://php.net/manual/en/function.mysql-query.php). –  Mar 05 '16 at 19:05
  • You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'order WHERE o_date BETWEEN SUBDATE( CURDATE( ) , DAYOFMONTH( CU' at line 2 – Rohan Khude Mar 05 '16 at 19:50
  • order is taking as a part of sql keyword not as table_name . please make `order` to `orders` – Rohan Khude Mar 05 '16 at 19:50
  • Tried using mysqli_query but still getting the same error.Also, the order table has a different name I have changed it here just for privacy issue. – Shweta Soparkar Mar 05 '16 at 20:26
  • I have used PDO for connecting to database. and it says that the connection is successful. – Shweta Soparkar Mar 05 '16 at 20:27
  • You shouldn't mix `mysql` functions, `mysqli` functions and `PDO`. They are [different APIs](http://php.net/manual/en/mysqlinfo.api.choosing.php) used to connect to `MySQL` databases. There is nothing wrong (but very unusual) to use two or all three of them in the same project but they **cannot be combined**. If you open your connection with [`mysql_connect()`](http://php.net/manual/en/function.mysql-connect.php) the you have to use the created connection only with [`mysql_*()`](http://php.net/manual/en/ref.mysql.php) functions. The same for the other two. – axiac Mar 05 '16 at 20:48

3 Answers3

2

Taken from comments, and reopened the question since you changed the table's name.

"order is taking as a part of sql keyword not as table_name . please make order to orders – Rohan Khude 46 mins ago"

OP:

"Tried using mysqli_query but still getting the same error.Also, the order table has a different name I have changed it here just for privacy issue. – Shweta Soparkar 10 mins ago"

and:

"I have used PDO for connecting to database. and it says that the connection is successful. – Shweta Soparkar 10 mins ago"

You cannot mix MySQL APIs, you must use the same one from connecting to querying.

  • Connect with PDO, query with PDO, nothing else.

You are using mysql_query() then mysqli_error() and mixing those together along with PDO.

Since you are using PDO to connect with, then you need to change your query to a PDO method.

Manual reference on querying with PDO:

Error checking references:

Other references:

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
0

Order is a reserved SQL keyword. You can get around this by wrapping it in backticks (`order`).

See https://dev.mysql.com/doc/refman/5.7/en/keywords.html for a full list of reserved keywords in MySQL 5.7.

Chelsea Urquhart
  • 1,388
  • 1
  • 11
  • 18
0
$STH_SELECT = $conn->query("SELECT SUM( usd_amount ) AS value_sum 
     FROM order_tbl 
     WHERE o_date BETWEEN SUBDATE( CURDATE( ) , 
          DAYOFMONTH( CURDATE( ) ) -1 ) AND CURDATE()
");
$sof = $STH_SELECT->fetchColumn();
echo $sof;
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
  • 1
    If you are not accessing the result set value by referencing `value_sum`, then there is no point declaring the column alias. This answer is missing its educational explanation. – mickmackusa Nov 14 '20 at 05:30