我正试图使用传递到URL
的和id
来编辑查询,但在尝试运行代码时,我得到了一个错误:
调用非对象上的成员函数where()
控制器
class HomeController extends BaseController {
public function showWelcome() {
$id = intval($_GET['wab_id']);
$results = DB::Table('films')->get()->where('wab_id','=', $id);
print_r($results);
while ($row=mysql_fetch_array($results)) {
$url = $row['url'];
}
return View::make('hello')->with('row', $url);
}
}
我也试过:
class HomeController extends BaseController {
public function showWelcome() {
$id = intval($_GET['wab_id']);
$results = DB::Table('films')->get()->where('wab_id', $id);
print_r($results);
while ($row=mysql_fetch_array($results)) {
$url = $row['url'];
}
return View::make('hello')->with('row', $url);
}
}
只被抛出相同的错误消息,这是唯一一个没有抛出错误但返回错误的代码段:
mysql_fetch_array()期望参数1是给定的资源、对象
class HomeController extends BaseController {
public function showWelcome() {
$id = intval($_GET['wab_id']);
$results = DB::Table("SELECT * FROM next WHERE wab_id=$id");
while ($row=mysql_fetch_array($results)) {
$url = $row['url'];
}
return View::make('hello')->with('row', $url);
}
}
路线
Route::get('/', array(
'as' => 'home',
'uses' => 'HomeController@showWelcome'
));
您需要更改链接的顺序,将where()函数放在get()函数之前。get()之后的任何内容都将应用于集合,而不是查询生成器。
如果您想通过URL传递参数,请尝试:
// routes.php
Route::get('/{wab_id}', array(
'as' => 'home',
'uses' => 'HomeController@showWelcome'
));
此外,where()约束应该在get()
方法之前。
现在,您应该能够接受URL参数作为HomeController#ShowWelcome
函数的参数
class HomeController extends BaseController {
public function showWelcome($wab_id) {
$id = $wab_id;
// where() constraint before the `get()` method.
$results = DB::Table('films')->where('wab_id','=', $id)->get();
print_r($results);
while ($row=mysql_fetch_array($results)) {
$url = $row['url'];
}
return View::make('hello')->with('row', $url);
}
}
我试图将一个std Obj数组传递给视图,但我必须设置wab_id,因为如果不设置,我将在下一个i中获得一个未定义的wab_id索引$arrays = json_decode(json_encode($results), true);
,以便将我使用查询获得的std数组转换为多数组,然后循环通过该数组,这就起到了的作用
感谢大家的帮助。
控制器
class HomeController extends BaseController {
public function showWelcome(){
$id = isset($_GET['wab_id'])?$_GET['wab_id']:"";
$results = DB::Table('films')->where('wab_id', $id)->get();
$arrays = json_decode(json_encode($results), true);
foreach ( $arrays as $film ) {
}
return View::make('hello')->with('film', $film);
}
}
您会得到一个mysql_fetch_array错误,因为当您打乱了get()和where()函数的顺序时
//you did it like this
DB::table('films')->get()->where('wab_id','=', $id);
//you did it like this, instead of
DB::table('films')->where('wab_id','=', $id)->get();
get()被忽略。这就是为什么结果不会是一个数组;
编辑:
如果使用toArray()辅助函数,则不需要将其编码和解码为Json。
//you can do so like this
DB::Table('films')->where('wab_id','=', $id)->toArray();
//or this if thats too long
$result = DB::table('films')->where('wab_id','=', $id);
$array = $result->toArray();