php - mysqli - Using SELECT FOUND_ROWS() with prepared statements -


i wondering how include select found_rows() existing query using limit, or if there better way total row count without limit.

//select * question  $q = $this->db->mysqli->prepare("select sql_calc_found_rows *                                  countries_ship c                                  join items                                 on c.item_id = i.id                                 join item_expire e                                 on c.item_id = e.item_id                                 join users u                                 on i.user_id = u.id                                 left join bids b                                 on i.id = b.item_id                                 left join publishers p                                 on i.item_publisher = p.id                                 left join tags_rel tr                                 on c.item_id = tr.item_id                                 join tags t                                 on t.id = tr.tag_id                                 left join countries co                                 on i.item_location = co.id                                 ".$where."             group i.id order ".$order." ".$limit."");      $count_rows = $this->db->mysqli->query("select found_rows()");  /* return of var_dump($count_rows); object(mysqli_result)#74 (5) {   ["current_field"]=>   int(0)   ["field_count"]=>   int(1)   ["lengths"]=>   null   ["num_rows"]=>   int(1)   ["type"]=>   int(0) } */  $prep = join("", $prep);      call_user_func_array('mysqli_stmt_bind_param', array_merge ( array($q, $prep), $this->helperclass->valref($ref) ) );      $q->execute();     $rows = $this->helperclass->bindresults($q);      $q->close();      return $rows; 

not sure if 100% efficient how solved problem.

  1. changed beginning of query select sql_calc_found_rows
  2. created new array - $resarray = array()
  3. moved second query suggested , added both results $resarray.

            $q->execute();          $rows = $this->helperclass->bindresults($q);          $q->close();          $count_rows = $this->db->mysqli->query("select found_rows()");          $count = mysqli_fetch_assoc($count_rows);          $resarray[] = $count;         $resarray[] = $rows;          return $resarray; 

you on right track sql_calc_found_rows, need move execute query counting resutls. valid after run original query. so, move $count_rows = $this->db->mysqli->query("select found_rows()"); after $q->execute();


Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -