function getFile($limit) { global $mysqli; connectDB(); $result = $mysqli->query("SELECT * FROM `office`.`tbl_uploads` ORDER BY `ID` DESC LIMIT {$limit}"); closeDB(); return resultToArray($result); }
function getList($table, $page, $pageSize) { global $mysqli; connectDB(); $first = ($page - 1) * $pageSize; $result = $mysqli->query("SELECT * FROM {$table} ORDER BY id DESC LIMIT {$first}, {$pageSize}"); closeDB(); return resultToArray($result); }
function Query($sql, $backtrace, $database = "contacts") { /*Show or hide queries*/ isset($_SESSION['debug']) && $_SESSION['debug'] == "on" ? $debug = 1 : ($debug = 0); /*Database connect*/ include 'connection.php'; $response = NULL; /*Start timer*/ $msc = microtime(true); if ($result = $mysqli->query($sql)) { /*Successful Query execution*/ //dump($mysqli,"mysqli"); /*Stop timer*/ $msc = number_format(microtime(true) - $msc, 3); /*increasing number of queries*/ isset($_SESSION['Queries']) ? $_SESSION['Queries']++ : ($_SESSION['Queries'] = 1); if ($result !== true) { /*For SELECT queries*/ //dump($result,"result"); /*If exists records then fetch this.*/ if ($result->num_rows) { $response = resultToArray($result); } /*Show SELECT query*/ if ($debug) { showQuery("FETCH", $backtrace, $mysqli->host_info, $database, $sql, $result->num_rows ? $result->num_rows : 0, $mysqli->affected_rows, $msc, $result->num_rows ? $response : NULL); } } else { /*For INSERT, UPDATE, DELETE queries*/ $response = $mysqli->insert_id ? $mysqli->insert_id : $mysqli->affected_rows; if ($debug) { showQuery("AFFECT", $backtrace, $mysqli->host_info, $database, $sql, $mysqli->insert_id, $mysqli->affected_rows, $msc, $response); } } } else { /*Query execution failed!*/ $msc = number_format(microtime(true) - $msc, 2); /*Show failure query*/ showQuery("FAILURE", $backtrace, $mysqli->host_info, $database, $sql, 0, 0, $msc, $mysqli->error); /*Return error message*/ $response = getSqlErrors($mysqli->error_list); } /*Close database connection*/ $mysqli->close(); /*Return records in tabular, nothing if not exists records or a message if it fails.*/ return $response; }
<?php $nr_items_pp = 3; $offset = ($page_nr - 1) * $nr_items_pp; $sql = "SELECT * FROM newsarticles LIMIT " . $offset . ", " . "{$nr_items_pp}"; $result = $mysqli->query($sql); $result = resultToArray($result);
<?php // include 'includes/header.php'; function resultToArray($result) { $rows = array(); while ($row = $result->fetch_assoc()) { $rows[] = $row; } return $rows; } $query = "SELECT tournament_id FROM user_tournaments WHERE user_id = '" . $_SESSION['user_id'] . "'"; $result = $db_connect->query($query); $rows = resultToArray($result); $result->free(); $row_id = array(); for ($x = 0; $x < count($rows); $x++) { $row_id[] = $rows[$x]['tournament_id']; } $comma_separated = implode(",", $row_id); $query = "SELECT tournament_name, tournament_id FROM tournament WHERE tournament_id IN ({$comma_separated})"; $result = $db_connect->query($query); $num_rows = mysqli_num_rows($result); if ($num_rows > 1) { ?> <div id="tour_select"> <h2>Välj turnering</h2> <select id="tournament_select" name="selected_tournament"> <?php while ($row = mysqli_fetch_assoc($result)) { ?>
<?php session_start(); require './sql_connect.php'; $amount = $mysqli->real_escape_string(filter_input(INPUT_GET, 'amount')); if ($amount == "" or $amount > 200) { exit("Error"); } function resultToArray($result) { $results_array = array(); while ($row = $result->fetch_array()) { $results_array[] = $row; } return $results_array; } $counter = $mysqli->query("SELECT COUNT(*) AS counter FROM `QuestionsB`"); $count = mysqli_fetch_array($counter)['counter']; $result = $mysqli->query("SELECT v_value FROM globals WHERE v_name='q2_offset'"); $offset = mysqli_fetch_assoc($result)['v_value']; if ($offset + $amount > $count) { $offset = 0; } $new_offset = $offset + $amount; $mysqli->query("UPDATE globals SET v_value='{$new_offset}' WHERE v_name='q2_offset'"); $result = $mysqli->query("SELECT * FROM `QuestionsB` LIMIT {$offset}, {$amount}"); $arr = resultToArray($result); print json_encode($arr);
<?php $search_string; $sql = "SELECT * FROM newsItem WHERE title LIKE '%{$search_string}%' "; $search_result = $mysqli->query($sql); $search_result = resultToArray($search_result);
<?php $sql = "SELECT * FROM movies"; $resultmovie = $mysqli->query($sql); $resultmovie = resultToArray($resultmovie); ?>
function search($keywords, $target, $db_connection) { $keywords = trim($keywords); if ($target == "all") { $mexact = movieExactQuery($keywords); $mclose = movieCloseQuery($keywords); $mpartial = moviePartialQuery($keywords); $resme = mysqli_query($db_connection, $mexact); $resmc = mysqli_query($db_connection, $mclose); $resmp = mysqli_query($db_connection, $mpartial); $rme = resultToArray($resme); $rmc = resultToArray($resmc); $rmp = resultToArray($resmp); $aexact = peopleExactQuery($keywords, "actor"); $apartial = peoplePartialQuery($keywords, "actor"); $resae = mysqli_query($db_connection, $aexact); $resap = mysqli_query($db_connection, $apartial); $rae = resultToArray($resae); $rap = resultToArray($resap); $dexact = peopleExactQuery($keywords, "director"); $dpartial = peoplePartialQuery($keywords, "director"); $resde = mysqli_query($db_connection, $dexact); $resdp = mysqli_query($db_connection, $dpartial); $rde = resultToArray($resde); $rdp = resultToArray($resdp); $arr = array("movie" => array("exact" => $rme, "close" => $rmc, "partial" => $rmp), "actor" => array("exact" => $rae, "partial" => $rap), "director" => array("exact" => $rde, "partial" => $rdp)); } elseif ($target == "movie") { $exact = movieExactQuery($keywords); $close = movieCloseQuery($keywords); $partial = moviePartialQuery($keywords); $rese = mysqli_query($db_connection, $exact); $resc = mysqli_query($db_connection, $close); $resp = mysqli_query($db_connection, $partial); $rme = resultToArray($rese); $rmc = resultToArray($resc); $rmp = resultToArray($resp); $arr = array("movie" => array("exact" => $rme, "close" => $rmc, "partial" => $rmp)); } elseif ($target == "actor") { $exact = peopleExactQuery($keywords, $target); $partial = peoplePartialQuery($keywords, $target); $rese = mysqli_query($db_connection, $exact); $resp = mysqli_query($db_connection, $partial); $rae = resultToArray($rese); $rap = resultToArray($resp); $arr = array("actor" => array("exact" => $rae, "partial" => $rap)); } elseif ($target == "director") { $exact = peopleExactQuery($keywords, $target); $partial = peoplePartialQuery($keywords, $target); $rese = mysqli_query($db_connection, $exact); $resp = mysqli_query($db_connection, $partial); $rde = resultToArray($rese); $rdp = resultToArray($resp); $arr = array("director" => array("exact" => $rde, "partial" => $rdp)); } elseif ($target == "people") { $aexact = peopleExactQuery($keywords, "actor"); $apartial = peoplePartialQuery($keywords, "actor"); $resae = mysqli_query($db_connection, $aexact); $resap = mysqli_query($db_connection, $apartial); $rae = resultToArray($resae); $rap = resultToArray($resap); $dexact = peopleExactQuery($keywords, "director"); $dpartial = peoplePartialQuery($keywords, "director"); $resde = mysqli_query($db_connection, $dexact); $resdp = mysqli_query($db_connection, $dpartial); $rde = resultToArray($resde); $rdp = resultToArray($resdp); $arr = array("actor" => array("exact" => $rae, "partial" => $rap), "director" => array("exact" => $rde, "partial" => $rdp)); } else { return json_encode(array("error" => "Invalid target.")); } return json_encode($arr); }
<?php $sql = "SELECT * FROM videos"; $resultvideo = $mysqli->query($sql); $resultvideo = resultToArray($resultvideo);
$app->group('/api', function () use($app, $mysqli) { // GET accounts list $app->get('/accounts', function ($request, $response, $args) use($app, $mysqli) { // Limit the output somewhat $sql = "SELECT * FROM accounts ORDER BY created_at DESC LIMIT 10000"; $result = $mysqli->query($sql); if (!$result) { //Error echo json_encode(array("code" => true, "resp" => array("errno" => $mysqli->errno, "message" => $mysqli->error))); } else { if ($result->num_rows === 0) { // No Results echo json_encode(array("code" => true, "resp" => array("message" => "No accounts exist"))); } else { // Success echo json_encode(array("code" => false, "resp" => resultToArray($result))); } } $result->free(); $mysqli->close(); }); // GET single account $app->get('/accounts/{id}', function ($request, $response, $args) use($app, $mysqli) { $sql = "SELECT * FROM accounts where id=" . $args['id']; $result = $mysqli->query($sql); if (!$result) { // Error echo json_encode(array("code" => true, "resp" => array("errno" => $mysqli->errno, "message" => $mysqli->error))); } else { if ($result->num_rows === 0) { // No Result
<?php $sql = "SELECT * FROM fotos"; $resultfotos = $mysqli->query($sql); $resultfotos = resultToArray($resultfotos);
{ $query = "SELECT * FROM articles ORDER BY date DESC LIMIT 5"; global $result; $result = $connection->query($query); //получаем объект result со всеми данными из таблицы articles if (!$result) { die($connection->error); } //return resultToArray($result); } //извлекаем данные из таблицы со статьями getNews($connection); //вызов функции написанной выше $rows = $result->num_rows; //из объекта берем свойство = числу строк $articles = resultToArray($result); /** * @param $result * @return array */ function resultToArray($result) { //global $array; $array = array(); //назначаем переменной пустой массив while ($row = $result->fetch_assoc()) { $array[] = $row; } return $array; } //echo print_r($articles); //отладка