简体   繁体   中英

mysql_query does not return anything but the query works fine in phpMyAdmin

$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.

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:

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.

$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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM