prepare($sql); //------------------------------------------ //Bind to query //------------------------------------------ if (!empty($criterias)){ foreach ($criterias as $key => $value){ $key_condition = ':'.$key; if (str_contains($sql, $key_condition)){ if ($key == 'search'){ $search_value = '%'.$value.'%'; $stmt->bindValue($key, $search_value, PDO::PARAM_STR); } elseif ($key == 'p'){ //Do nothing (bug) } else { $stmt->bindValue($key, $value, PDO::PARAM_STR); } } } } //------------------------------------------ // Debuglog //------------------------------------------ if (debug){ $message = $date.';'.$sql.';'.$username; debuglog($message); } //------------------------------------------ //Add paging details //------------------------------------------ $page_rows = $page_rows_equipment ?? 20; if(isset($criterias['totals']) && $criterias['totals']==''){ $stmt->execute(); $messages = $stmt->fetch(); $messages = $messages[0]; } elseif(isset($criterias['all']) && $criterias['all']==''){ //Return all records (no paging) $stmt->execute(); $messages = $stmt->fetchAll(PDO::FETCH_ASSOC); } else { $current_page = isset($criterias['p']) && is_numeric($criterias['p']) ? (int)$criterias['p'] : 1; $stmt->bindValue('page', ($current_page - 1) * $page_rows, PDO::PARAM_INT); $stmt->bindValue('num_rows', $page_rows, PDO::PARAM_INT); //Execute Query $stmt->execute(); //Get results $messages = $stmt->fetchAll(PDO::FETCH_ASSOC); } //------------------------------------------ //JSON_EnCODE //------------------------------------------ $messages = json_encode($messages, JSON_UNESCAPED_UNICODE); //------------------------------------------ //Send results //------------------------------------------ echo $messages; ?>