Syntax error or access violation – Laravel

huangapple go评论64阅读模式
英文:

Syntax error or access violation - Laravel

问题

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

"语法错误或访问违规: 1064 您的SQL语法错误;请检查与您的MariaDB服务器版本相对应的手册,以了解在第1行使用正确的语法 near 'to,amount,from,date,status,provider from api_transactions where to=?' (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);
}
英文:

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);
    }

答案1

得分: 1

to 在 MySQL 中是一个保留关键字。你应该使用反引号包裹 to

$client_data = DB::select('SELECT `merchant_name`, `to`, `amount`, `from`, `date`, `status`, `provider` from `api_transactions` WHERE `to`=?', [$merchant_id]);
英文:

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]);

huangapple
  • 本文由 发表于 2023年5月10日 16:31:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76216385.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定