-1

I do have table with the fields- BranchCode VARCHAR, TransactionDate DATE, TransactionId BIGINT And in PHP I have written sql query as

$sqllastTransId="select * from transactiontable where BranchCode ={$BranchCode} and DATE(TransactionDate) ={$TransDate} order by TransactionId desc limit 1";

I was trying to get last or maximum TransactionId on the particular date of the particular BranchCode But the result is NULL

DarkBee
  • 16,592
  • 6
  • 46
  • 58
  • 2
    _"But the result is NULL"_ - the result of _what_, exactly? Right now, you have only shown us how you assign a value to a string variable. – CBroe Aug 10 '23 at 12:47
  • 1
    Guessing most likely you query simply failed, because you neglected to put alphanumeric values into quotes. – CBroe Aug 10 '23 at 12:47
  • SELECT MAX(TransactionId) AS MaxTransactionId FROM transactiontable WHERE BranchCode = 'your_branch_code' AND DATE(TransactionDate) = 'your_transaction_date'; – Ravi Rathore Aug 10 '23 at 12:49
  • 1
    Required reading: [How to include a PHP variable inside a MySQL statement](https://stackoverflow.com/questions/7537377/how-to-include-a-php-variable-inside-a-mysql-statement) – ADyson Aug 10 '23 at 12:52
  • The same query is working fine in SQL but resulting NULL through php – sachin p Aug 10 '23 at 13:02
  • You've already been told why it's not working in PHP, and given a link with instructions showing how to fix it (using prepared statements and parameters, which will also make your code safe against SQL injection, as well as more reliable). – ADyson Aug 10 '23 at 14:24

0 Answers0