[转]Raw Queries in Laravel
本文转自:https://fideloper.com/laravel-raw-queries
Business logic is often complicated. Because of this, we often need to write our own SQL queries. Luckily, Laravel's query builder has the tools we need to safely run such queries.
A key concern when writing our own queries is protecting our application from SQL injection attacks. Normally, the query builder does this for us. However, when we write our own SQL, we need to make sure we don't inadvertently remove this protection.
Here's what we want to avoid:
$someVariable = Input::get("some_variable");
$results = DB::select( DB::raw("SELECT * FROM some_table WHERE some_col = '$someVariable'") );
In the above query, we're directly adding user input into the query without sanitizing it. This leaves us open to attack!
DB::raw()
is used to make arbitrary SQL commands which aren't parsed any further by the query builder. They therefore can create a vector for attack via SQL injection.
Since the query builder is using PDO in the background, we know there is a way to bind parameters to our query so it will sanitize the bound variables.
Now, as you've seen, arbitrary (raw) queries are done in the query builder using the DB::select()
method. Let's look at the select()
method in Illuminate\Database\Connection to see if it has any way to bind our parameters:
public function select($query, $bindings = array())
{
return $this->run($query, $bindings, function($me, $query, $bindings)
{
if ($me->pretending()) return array();
// For select statements, we'll simply execute the query and return an array
// of the database result set. Each element in the array will be a single
// row from the database table, and will either be an array or objects.
$statement = $me->getPdo()->prepare($query);
$statement->execute($me->prepareBindings($bindings));
return $statement->fetchAll($me->getFetchMode());
});
}
Perfect! We see above that we can pass an array of bindings to the select()
method. This array is bound to the query via the PDO connection.
We can, therefore, change our previous query in a way that sanitizes the user input:
$someVariable = Input::get("some_variable");
$results = DB::select( DB::raw("SELECT * FROM some_table WHERE some_col = :somevariable"), array(
'somevariable' => $someVariable,
));
Voìla! Safe queries!
Lastly, if you are performing queries which don't return data, then using a SELECT query will result in errors. For example, if you want to start the auto-increment ID of a MySQL table to something other than zero, we can use the statement
method. With statement
, we don't need to use the raw()
method:
// Warning: This is a MySQL-specific query
DB::statement( 'ALTER TABLE HS_Request AUTO_INCREMENT=9999' );
The statement
method can also accept parameters:
DB::statement( 'ALTER TABLE HS_Request AUTO_INCREMENT=:incrementStart', array('incrementStart' => 9999) );
[转]Raw Queries in Laravel的更多相关文章
- Executing Raw SQL Queries using Entity Framework
原文 Executing Raw SQL Queries using Entity Framework While working with Entity Framework developers m ...
- laravel 配置MySQL读写分离
前言:说到应对大流量.高并发的解决方案的时候,总会有这样的回答,如:读写分离,主从复制...等,数据库层今天先不讨论,那么今天我们就来看看怎么在应用层实现读写分离. 框架:laravel5.7(所有配 ...
- Sequelize-nodejs-11-Raw queries
Raw queries原始查询 就是使用了原始的查询语句,如UPDATE users SET y = 42 WHERE x = 12 As there are often use cases in w ...
- laravel3中文文档是迈入laravel4的捷径
http://v3.golaravel.com/docs/ 目录 Laravel概览 更新日志 安装与设置 系统需求 安装 服务器设置 基本设置 环境 友好的链接(URL) 路由 基础 通配符(Wil ...
- Laravel5.1学习笔记16 数据库2 查询构造器(这个不用看,不如用EloquentORM)
Introduction Retrieving Results Aggregates Selects Joins Unions Where Clauses Advanced Where Clauses ...
- Laravel5.1学习笔记15 数据库1 数据库使用入门
简介 运行原生SQL查询 监听查询事件 数据库事务 使用多数据库连接 简介 Laravel makes connecting with databases and running queries e ...
- code_action
w https://raw.githubusercontent.com/laravel/laravel/master/config/database.php <?php return [ /* ...
- Python札记 -- MongoDB模糊查询
最近在使用MongoDB的时候,遇到了使用多个关键词进行模糊查询的场景.竹风使用的是mongoengine库. 查了各种资料,最后总结出比较好用的方法.先上代码,后面进行详细说明.如下: #!/usr ...
- 【转】一个lucene的官网例子
创建索引: import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import jav ...
随机推荐
- python 类成员知识点学习的一个坑(初学者,大神请绕行)
先来段小程序class Foo: name = "abc" def __init__(self,age): self.age = age print(Foo.name)Foo.na ...
- 支持Linux,嗅探和注入功能的网卡
支持的WiFi USB 以下是已知可以很好地支持Linux,嗅探和注入功能,外部天线(可以替换)和强大的TX功率以及良好的RX灵敏度的Wifi卡的列表 TP-LINK TL-WN722N(仅限卷1) ...
- JAVA RSA非对称加密详解[转载]
一.概述1.RSA是基于大数因子分解难题.目前各种主流计算机语言都支持RSA算法的实现2.java6支持RSA算法3.RSA算法可以用于数据加密和数字签名4.RSA算法相对于DES/AES等对称加密算 ...
- MFC常见问题总结
1. c++中的函数前面加个LRESULT是什么意思啊?在微软vc提供的头文件中有定义在winnt.h中typedef long LONG;在windef.h中typedef LONG LRESULT ...
- W3bsafe]SQLmap过狗命令的利用+教程
W3bsafe]SQLmap过狗命令的利用+教程 本文转自:i春秋社区 我就是那个爱装逼的小人 本屌又来装逼了 SQLmap注入的时候 有的肯定会被安全狗拦截吧 本屌来教各位过狗!过waf等安全狗 ...
- Java核心技术卷一基础技术-第13章-集合-读书笔记
第13章 集合 本章内容: * 集合接口 * 具体的集合 * 集合框架 * 算法 * 遗留的集合 13.1 集合接口 Enumeration接口提供了一种用于访问任意容器中各个元素的抽象机制. 13. ...
- python zeros用法实例
编程就是踩坑的过程.今天又踩了一个坑,做个积累吧. 在给数组赋初始值的时候,经常会用到0数组,而Python中,我们使用zero()函数来实现.在默认的情况下,zeros创建的数组元素类型是浮点型的, ...
- 性能瓶颈之Mapping
如果Source和Target都不存在性能上的瓶颈,则问题可能会出在Mapping 如何判定Mapping存在性能瓶颈 1) 在session log中读取thread statistics和wor ...
- python基础-字符串(6)
一.引言 当打来浏览器登录某些网站的时候,需要输入密码,浏览器把密码传送到服务器后,服务器会对密码进行验证,其验证过程是把之前保存的密码与本次传递过去的密码进行对比,如果相等,那么就认为密码正确,否则 ...
- python之获取当前操作系统(平台)
Python在不同环境平台使用时,需要判断当前是什么系统,比如常用的windows,linux等 下面介绍一些能够获取当前系统的命令 1.使用sys.platform获取 #!/usr/bin/env ...