function listAction()
{
    $config = getConfig();
    $columnsName = [];
    foreach ($_GET['columns'] as $column) {
        if (!empty($column['data'])) {
            $columnsName[] = $column['data'];
        }
    }
    $order = $_GET['order'][0];
    $orderColumn = $order['column'];
    $start = intval($_GET['start']);
    $sqlAssoc = ['table' => ['name' => 'item', 'dbname' => 'db_vktest', 'as' => 'i'], 'query' => ['select' => $columnsName, 'from' => 'item', 'order' => ['column' => $columnsName[$orderColumn], 'dir' => $order['dir']], 'start' => $start, 'length' => $_GET['length']]];
    $mysqli = db_mysqli_connect($sqlAssoc['table']['dbname']);
    $rowStore = db_mysqli_query_fetch_store($mysqli, 'SELECT s.countitems FROM store AS s WHERE s.idstore = 1', MYSQLI_ASSOC)[0];
    $sqlQueryes = buildListItemQuery($sqlAssoc, $rowStore['countitems']);
    $rows = db_mysqli_query_fetch_list($mysqli, $sqlQueryes, MYSQLI_ASSOC);
    db_mysqli_close($mysqli);
    //    var_dump($_SESSION);
    //    var_dump($sqlQueryes);
    $_SESSION['list'] = ['lastitem' => $rows[count($rows) - 1], 'firstitem' => $rows[0], 'lastpage' => $start, 'slowQueryType' => $sqlQueryes['slowQueryType']];
    //for cahce before last result
    afterLictAction($sqlAssoc, $rowStore);
    $response = ['recordsTotal' => $rowStore['countitems'], 'recordsFiltered' => $rowStore['countitems'], 'data' => $rows, 'draw' => $_GET['draw'], 'start' => $_GET['start']];
    echo json_encode($response);
}
function createAction()
{
    $config = getConfig();
    $params = $config['memcache'];
    $table = ['name' => 'item', 'dbname' => 'db_vktest', 'as' => 'i'];
    $item = handleRequest($_POST);
    $mysqli = db_mysqli_connect($table['dbname']);
    $queryInsert = "INSERT INTO item (name,description,price,url) VALUES (" . "'" . mysqli_real_escape_string($mysqli, $item['name']) . "'," . "'" . mysqli_real_escape_string($mysqli, $item['description']) . "'," . $item['price'] . "," . "'" . mysqli_real_escape_string($mysqli, $item['url']) . "'" . ")";
    $resultInsert = mysqli_query($mysqli, $queryInsert);
    $resultError = mysqli_error($mysqli);
    db_mysqli_close($mysqli);
    if (!$resultInsert) {
        addAlert('danger', 'Произошла ошибка записи:' . $resultError);
        $url = 'http://' . $_SERVER['HTTP_HOST'] . "/";
        header('Location: ' . $url);
        exit;
    }
    //another proc
    $pid = pcntl_fork();
    if ($pid == 0) {
        changeCountItemsById(1, 1);
        exit(0);
    }
    addAlert('success', 'Продукт добавлен');
    $url = 'http://' . $_SERVER['HTTP_HOST'] . "/";
    header('Location: ' . $url);
}
function getItemsByIds($ids)
{
    $table = ['name' => 'item', 'dbname' => 'db_vktest', 'as' => 'i'];
    $sqlQuery = "SELECT * FROM item WHERE iditem IN (" . implode(',', $ids) . ')';
    $mysqli = db_mysqli_connect($table['dbname']);
    $result = mysqli_query($mysqli, $sqlQuery);
    $rows = [];
    while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
        $rows[] = $row;
    }
    db_mysqli_close($mysqli);
    return $rows;
}
function changeCountItemsById($id, $val)
{
    $config = getConfig();
    $params = $config['memcache'];
    $table = ['name' => 'item', 'dbname' => 'db_vktest', 'as' => 'i'];
    $mysqli = db_mysqli_connect($table['dbname']);
    $queryUpdate = 'UPDATE store SET countitems = countitems + ' . intval($val) . ' WHERE idstore = ' . intval($id);
    $resultUpdate = mysqli_query($mysqli, $queryUpdate);
    db_mysqli_close($mysqli);
    $memcache = memcache_connect($params['host'], $params['port']);
    memcache_flush($memcache);
    memcache_close($memcache);
}
function afterLictAction($queryAssoc, $rowStore)
{
    $config = getConfig();
    /**
     * Count of items page,that need be cached,because
     * but last-1/-2/-3/-4 use slow query
     */
    $COUNT_BEFORE_END = 5;
    $length = $queryAssoc['query']['length'];
    $itemsLast = $rowStore['countitems'] % $length;
    $lastPage = $rowStore['countitems'] - $itemsLast;
    $start = $queryAssoc['query']['start'];
    /**
     * only for last page
     */
    if ($start == $lastPage) {
        //last 5 page in cache&
        $queryCheckPre = array_merge($queryAssoc, []);
        $queryCheckPre['query']['start'] = $start - 1 * $length;
        $queryPre = buildListItemQuery($queryCheckPre, $rowStore['countitems']);
        $params = $config['memcache'];
        $memcache = memcache_connect($params['host'], $params['port']);
        $memcacheQueryKey = 'QUERY' . $queryPre['slow'];
        $data = memcache_get($memcache, $memcacheQueryKey);
        memcache_close($memcache);
        //another proc
        if (empty($data)) {
            $pid = pcntl_fork();
            if ($pid == 0) {
                $mysqli = db_mysqli_connect($queryAssoc['table']['dbname']);
                for ($i = 1; $i < $COUNT_BEFORE_END; $i++) {
                    $queryAssoc['query']['start'] = $start - $i * $length;
                    $sqlQueryes = buildListItemQuery($queryAssoc, $rowStore['countitems']);
                    $rows = db_mysqli_query_fetch_list($mysqli, $sqlQueryes, MYSQLI_ASSOC);
                    $_SESSION['list'] = ['lastitem' => $rows[count($rows) - 1], 'firstitem' => $rows[0], 'lastpage' => $queryAssoc['query']['start'], 'slowQueryType' => $sqlQueryes['slowQueryType']];
                    //
                    usleep(500);
                }
                db_mysqli_close($mysqli);
                exit(0);
                //end new proc
            }
        }
    }
}
function updateAction()
{
    $config = getConfig();
    $table = ['name' => 'item', 'dbname' => 'db_vktest', 'as' => 'i'];
    $item = handleRequest($_POST);
    $mysqli = db_mysqli_connect($table['dbname']);
    $queryUpdate = "UPDATE item SET " . "name='" . mysqli_real_escape_string($mysqli, $item['name']) . "'," . "description='" . mysqli_real_escape_string($mysqli, $item['description']) . "'," . "price=" . $item['price'] . "," . "url='" . mysqli_real_escape_string($mysqli, $item['url']) . "'" . " WHERE iditem=" . $item['iditem'];
    $resultUpdate = mysqli_query($mysqli, $queryUpdate);
    $resultError = mysqli_error($mysqli);
    db_mysqli_close($mysqli);
    if (!$resultUpdate) {
        addAlert('danger', 'Произошла ошибка записи:' . $resultError);
        $url = 'http://' . $_SERVER['HTTP_HOST'] . "/";
        header('Location: ' . $url);
        exit;
    }
    addAlert('success', 'Продукт сохранен!');
    $url = 'http://' . $_SERVER['HTTP_HOST'] . "/";
    header('Location: ' . $url);
}
function deleteItemsByIds($ids)
{
    $table = ['name' => 'item', 'dbname' => 'db_vktest', 'as' => 'i'];
    $mysqli = db_mysqli_connect($table['dbname']);
    $sqlQuery = "DELETE FROM item WHERE iditem IN (" . implode(',', $ids) . ')';
    $result = mysqli_query($mysqli, $sqlQuery);
    $_return = $result;
    if (($deleteRows = mysqli_affected_rows($mysqli)) <= 0) {
        $_return = false;
    }
    var_dump($_return);
    var_dump($sqlQuery);
    var_dump($deleteRows);
    db_mysqli_close($mysqli);
    if ($_return) {
        $pid = pcntl_fork();
        if ($pid == 0) {
            changeCountItemsById(1, $deleteRows * -1);
            exit;
        }
    }
    return $_return;
}
Exemple #8
0
function run_insert($query)
{
    $conn = db_mysqli_connect();
    if ($conn) {
        if ($_GET['debug'] != '' || $_POST['debug'] != '' || $_SESSION['debug'] != '') {
            log_to_db($query);
        }
        $result = $conn->query($query);
        if (!$result) {
            $trace_data = debug_backtrace();
            log_error("Unable to execute query: {$query}\n" . $conn->error, LOG_TO_ALL, "Unable to execute query.", $trace_data['file'] . ':' . $trace_data['function'] . ':' . $trace_data['line']);
        } else {
            return true;
        }
    }
}
 function insert($input_row)
 {
     $error_string = htmlentities($input_row['error_string'], ENT_QUOTES);
     $error_type = htmlentities($input_row['error_type'], ENT_QUOTES);
     $environment_string = htmlentities($input_row['environment_string'], ENT_QUOTES);
     $conn = db_mysqli_connect();
     $insert = "insert into errorlog\r\n                    \t( errorlog_timestamp, \r\n                    \t  errorlog_type, \r\n                    \t  errorlog_string, \r\n                    \t  errorlog_status, \r\n                    \t  errorlog_environment)\r\n                    \tvalues\r\n                    \t( CURRENT_TIMESTAMP, \r\n                    \t  '{$error_type}', \r\n                    \t  '{$error_string}', \r\n                    \t  'new', \r\n                    \t  '{$environment_string}' )";
     if ($conn) {
         $result = $conn->query($insert);
         if (!$result) {
             //log_error( "<b>ERROR:</b>Unable to insert error record into the database:$insert", LOG_TO_FILE | LOG_TO_DISPLAY, "An error was encountered attempting to save information to the error log database." );
             //error_log("\n$insert\n", 3, LOG_FILE);
             log_to_file($insert);
             return false;
         }
         mysqli_close($conn);
         return true;
     } else {
         return false;
     }
 }