我有一个推荐表,我想在我的 Laravel 4 项目的主页上显示它。通常我会运行一个查询来获取随机行:
SELECT * FROM `testimonials` WHERE `id`=".mt_rand(1,3);
但是在尝试运行它时出现此错误:
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '".mt_rand(1,3)' at line 1 (SQL: SELECT * FROM `testimonials` WHERE `id`=".mt_rand(1,3);) (Bindings: array ( 0 => 1, ))
这是我的控制器:
public function showHome()
{
DB::select('SELECT * FROM `testimonials` WHERE `id`=".mt_rand(1,3);', array(1));
return View::make('home.index', array('pageTitle' => 'Home'));
}
另一个问题是如何在 home.blade.php 模板中显示这些信息?
我通常会做一个while循环并做一些类似 $row['assoc_array']