Skip to content

Instantly share code, notes, and snippets.

View YaroslavB's full-sized avatar
🏠
Working from home

YaroslavB YaroslavB

🏠
Working from home
  • Kiev/Ukraine
View GitHub Profile
@YaroslavB
YaroslavB / rendom_values.sql
Created February 27, 2020 10:04
random values from large MySQL tables by condition, the random_seed table is added, containing the id and random_seed fields, filled with random numbers, an index is added to this column, and the index is added to the column by which the selection will take place. Now, to select random strings by condition, the query needs to be changed as follo…
SELECT
u1.*
FROM
users u1,
random_seed rs
WHERE
u1.role_id=5 AND u1.id=(rs.id+random_from_php)
ORDER BY
rs.random_seed
LIMIT 10
<?php
$servers = [
['Сервер 1', 70],
['Сервер 2', 55],
['Сервер 3', 30],
];
$pickServer = function () use ($servers) {
// Поиск и выбор подходящего сервера
/**
* @param array|\stdClass $array
*/
function walk($array)
{
//convert object to key-value array
if (is_object($array)) {
$array = (array)$array;
}
@YaroslavB
YaroslavB / hierarchical_table.sql
Last active November 12, 2019 09:53
MySQL Closure Table hierarchical database - How to pull information out in the correct order
use test_db;
-- Simple Sample
SET FOREIGN_KEY_CHECKS=0;
DROP TRIGGER IF EXISTS Tree_Insert;
DROP TRIGGER IF EXISTS Tree_Update;
DROP TABLE IF EXISTS TreePaths;
DROP TABLE IF EXISTS TreeData;
SET FOREIGN_KEY_CHECKS=1;

Этот урок переехал по адресу https://github.com/codedokode/pasta/blob/master/db/databases.md . Копия ниже устарела и не будет больше обновляться.


Что такое базы данных

База данных - это хранилище, в которое можно сохранять данные, а позже делать по ним поиск и загружать их. Ну например, на форуме в базе данных может храниться информация о пользователях сайта и написанных ими сообщениях. При просмотре страницы скрипт на сервере ищет в БД сообщения на определенную тему и выводит их на странице. Почти любой интерактивный сайт использует БД.

Конечно, можно попробовать сделать свое хранилище (к примеру, на файлах), но вряд ли оно будет работать так же быстро и надежно, как профессиональная база данных. Хорошая база данных гарантирует отсутствие потерь сохраненных данных, даже если неожиданно отключится питание, отсутствие проблем при одновременной работе нескольких пользователей, позволяет искать информацию по произвольным критериям.

@YaroslavB
YaroslavB / npm-commands.md
Last active December 19, 2018 10:31 — forked from ankurk91/npm-commands.md
Useful npm commands and tricks

npm v3.10 - ◾

⚠️ This gist is outdated, but most of the commands are still relevant.

Update npm itself

npm install -g npm
# Downgrade to a specific version
npm install -g npm@2
@YaroslavB
YaroslavB / 0. nginx_setup.sh
Created May 2, 2018 14:45 — forked from mikhailov/0. nginx_setup.sh
Nginx + secure pseudo-streaming
# Nginx can serve FLV/MP4 files by pseudo-streaming way without any specific media-server software.
# To do the custom build we use 2 modules: --with-http_secure_link_module --with-http_flv_module
# This module "secure-link" helps you to protect links from stealing away.
#
# NOTE: see more details at coderwall: http://coderwall.com/p/3hksyg
cd /usr/src
wget http://nginx.org/download/nginx-1.5.13.tar.gz
tar xzvf ./nginx-1.5.13.tar.gz && rm -f ./nginx-1.5.13.tar.gz
@YaroslavB
YaroslavB / nginx.conf
Created March 12, 2018 15:01 — forked from biqqthiqq/nginx.conf
Best nginx configuration for improved security(and performance). Complete blog post here http://tautt.com/best-nginx-configuration-for-security/
# to generate your dhparam.pem file, run in the terminal
openssl dhparam -out /etc/nginx/ssl/dhparam.pem 2048
@YaroslavB
YaroslavB / gist:d90739f32c9c226b48dbe9d5e559d4a5
Created March 12, 2018 14:14 — forked from prime31/gist:5675017
Simple PHP script showing how to send an Android push notification. Be sure to replace the API_ACCESS_KEY with a proper one from the Google API's Console page. To use the script, just call scriptName.php?id=THE_DEVICE_REGISTRATION_ID
<?php
// API access key from Google API's Console
define( 'API_ACCESS_KEY', 'YOUR-API-ACCESS-KEY-GOES-HERE' );
$registrationIds = array( $_GET['id'] );
// prep the bundle
$msg = array
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="[add your bin description]">
<meta charset="utf-8">
<title>JS Bin</title>
<link href="https://fonts.googleapis.com/css?family=Catamaran" rel="stylesheet">
<style id="jsbin-css">
*, *:after, *:before {
box-sizing: border-box;