Friday, March 3, 2017

Laravel: Check if MySQL query has result

First Laravel project. I have a controllerfunction what checks if there any record with that barcode. If no insert a record. If yes add one for the count.

public function sellmode(Request $request){
    $barcode=$request->barcode;
    $test = DB::select('select count from sellmode where barcode = ?', [$barcode]);
    $row_cnt = mysqli_num_rows($test);
    if ($row_cnt == 0) {
        Debugbar::info('Új sor');
        DB::insert('insert into sellmode (barcode, count) VALUES (?, ?)', [$barcode, '1']); 
    } else {
        Debugbar::info('Meglévő frissítése');
        DB::update('update sellmode set count = count + 1 WHERE barcode = ?' [$barcode]);
    }
    return view(sell);

}

When I tried it, it had the following error:

ErrorException in SellController.php line 17: mysqli_num_rows() expects parameter 1 to be mysqli_result, array given

What did I wrong?



via Feralheart

Advertisement