php - 如何将 PHP 变量传递给 Laravel DB::未准备好的查询

标签 php mysql laravel-5.1

使用 Laravel 5.1。我在下面有这个查询。它在 phpMyAdmin 中运行良好。如果我对 $category$value 进行硬编码,它也会起作用。但是,没有多少变量的连接和/或转义可以使查询在我的 Controller 中运行。我正在使用 use Illuminate\Support\Facades\DB; 我尝试用单引号和双引号封装查询,但没有效果。我正在使用 PHPStorm,它甚至说查询是正确的,但事实并非如此。我究竟做错了什么 ??

public function addNode(Request $request)
{

$category = $request->input('parent_category');
$value = $request->input('new_category');


$result = DB::unprepared("
LOCK TABLE nested_categories WRITE;
SELECT @myLeft := lft FROM nested_categories
WHERE name =  '.\'$category\'.';
UPDATE nested_categories SET rgt = rgt + 2 WHERE rgt > @myLeft;
UPDATE nested_categories SET lft = lft + 2 WHERE lft > @myLeft;
INSERT INTO nested_categories(name, lft, rgt) VALUES( '.\'$value\'.', @myLeft + 1, @myLeft + 2);
UNLOCK TABLES;");

return redirect('nested_categories');
}

谢谢!!

最佳答案

知道怎么做就很容易。忘记连接或反斜杠。只需使用这个:

$category = $request->input('parent_category');
$value = $request->input('new_category');

$result = DB::unprepared("
LOCK TABLE nested_categories WRITE;
SELECT @myLeft := lft FROM nested_categories
WHERE name =  '$category';
UPDATE nested_categories SET rgt = rgt + 2 WHERE rgt > @myLeft;
UPDATE nested_categories SET lft = lft + 2 WHERE lft > @myLeft;
INSERT INTO nested_categories(name, lft, rgt) VALUES( '$value', @myLeft + 1, @myLeft + 2);
UNLOCK TABLES;");

关于php - 如何将 PHP 变量传递给 Laravel DB::未准备好的查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40731548/

相关文章:

php - 我的术语已被搜索,但我的 OR 语句没有收到任何结果

php - 创建 Excel 文件的最有效方法

c++ - 如何为qt构建mysql驱动程序?

php - 有没有关于如何在 "Laravel 5.1"中为基于 CRUD 的应用程序编写测试的示例?

php - 如何使用php保存图片?

php - 如何在 codeigniter 中的两个 OR_LIKE 查询之间执行 AND 运算

javascript - sequelize - 获取包含模型的 AVG

android - 如何使用jdbc将android studio连接到mysql

php - laravel 5.1 中的 Eloquent 和 Query Builder 有什么区别?

php - Laravel 5.2.31 中 API 的 TokenMismatchException