0

I am trying to select data from the database, unfortunelety I am getting below error :

"Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'to,amount,from,date,status,provider from api_transactions where to=?' at line 1 (SQL: SELECT merchant_name,to,amount,from,date,status,provider from api_transactions where to=00000) in file /home/nosi/myProjects/paylesotho/vendor/laravel/framework/src/Illuminate/Database/Connection.php on line 664 "

Below is what I did inside the controller:

public function TransactionDetails ($merchant_id){
        $client_data = DB::select('SELECT merchant_name,to,amount,from,date,status,provider from api_transactions where to=?', [$merchant_id]);
        return response()->json($client_data);
    }
  • [When to use single quotes, double quotes, and backticks in MySQL](https://stackoverflow.com/questions/11321491/when-to-use-single-quotes-double-quotes-and-backticks-in-mysql) – brombeer May 10 '23 at 08:48
  • If you use laravel, then why do you use a simple raw query rather than the built-in ORM to get the data? I understand the use of raw sql for a very complicated report with subqueries functions and aggregations. – Shadow May 10 '23 at 09:27

1 Answers1

1

to is a reserved keyword in mysql. You should wrap to in backticks.

$client_data = DB::select('SELECT `merchant_name`,`to`,`amount`,`from`,`date`,`status`,`provider` from `api_transactions` WHERE `to`=?', [$merchant_id]);
Refilon
  • 3,334
  • 1
  • 27
  • 51