[转]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 ...
随机推荐
- C++ Error C2664:无法将参数 1 从“const char [9]”转换为“LPCWSTR”解决方案
问题出现 编译平台:VS2013 Windows 出现地方:在使用LoadLibrary( )函数动态链接DLL文件时出现的一个问题 Eg. 在使用 UNICODE字符的工程中, HIN ...
- POJ2248-Addition Chains
满足如下条件的序列被称为加成序列: X[1]=1,X[m]=n,X[1]<X[2]<......<X[m-1]<X[n] 对于每个k(2<=k<=m)都存在两个整数 ...
- 前端基础之 html
---恢复内容开始--- web服务本质 import socket sock=socket.socket() sock.bind(()) sock.listen() where True: conn ...
- Linux下安装GEOS环境
1.下载对应版本的geos源码:http://download.osgeo.org/geos/ 2.下载后使用cd切换到源码目录解压:tar -xvf geosXXX.tar.gz 3.切换到解压后目 ...
- HTB Linux queuing discipline manual - user guide笔记
1. Introduction HTB is meant as a more understandable, intuitive and faster replacement for the CBQ ...
- MySQL--MHA与GTID
##==========================================## MySQL 5.6版本引入GTID来解决主从切换时BINLOG位置点难定位的问题,MHA从0.56版本开始 ...
- JavaScript工作体系中不可或缺的函数
一.函数的概念 日常生活中,我们要完成一件事,总是习惯先有一个计划,后期按照计划,一步一步执行,则能够完成,并且达到一定效果实现一定的功能.在编程的世界里,“功能”可称呼为“函数”,因此“函数”即一段 ...
- BATJ等公司必问的8道Java经典面试题,你都会了吗?
1.谈谈你对 Java 平台的理解?“Java 是解释执行”,这句话正确吗? 考点分析: 对于这类笼统的问题,你需要尽量表现出自己的思维深入并系统化,Java 知识理解得也比较全面,一定要避免让面试官 ...
- 开源播放器 ijkplayer (五) :Linux/Ubuntu 下编译ijkplayer
一.安装Git与yasm sudo apt-get install git sudo apt-get install yasm 二.下载和配置 SDK.NDK SDK一般开发时肯定都有的,NDK一般是 ...
- Android JNI 学习(三):JNI 数据类型和数据结构
本文我们来讨论一下JNI如何将Java类型映射到本机C类型. 一.基本数据类型 如下图表整理了Java基本类型和native对应的关系: Java类型 Native类型 描述 boolean jboo ...