Saturday, December 31, 2016

Laravel insert ignore check if inserted (affected rows for insert ignore)

Halo guys!
if you break your head for finding solution to check if row is inserted here is the way

$query = "INSERT INTO table (id) VALUES (?)";
    try {
        DB::insert($query, array($value));
        return 1;
    } catch (\Exception $e) {
        return 0;
    }

The solution is simle - not use insert ignore. So we are catching exception instead of fatal erroring and know if it did or not.
You can even do query builder insert instead of raw query.

Sunday, November 13, 2016

Wednesday, October 5, 2016

Installing mail (sendmail) Ubuntu 16

sudo apt-get install sendmail
And now you can send mails easy with tls enabled without any other bullshit configs

Monday, October 3, 2016

Laravel 5.3 Nginx Config

    server {
            listen 80;
            listen [::]:80;

            root /var/www/laravel/public;
            index index.php index.html index.htm;

            server_name my_app.dev;

            location / {
                    try_files $uri $uri/ /index.php?$query_string;
            }
            location ~ \.php$ {
              include snippets/fastcgi-php.conf;
              fastcgi_pass unix:/run/php/php7.0-fpm.sock;
            }

            location ~ /\.ht {
                deny all;
            }

Sunday, October 2, 2016

Reload MySql

This should work on ubuntu 18.04
sudo service mysql restart

If not you can try other commands:
sudo systemctl restart mysqld
sudo systemctl restart mysql
or

sudo service mysqld restart
sudo service mysql restart
or

sudo /etc/init.d/mysql reload
or
sudo /etc/init.d/mysql force-reload

Change Auto Increment counter in MySQL

ALTER TABLE tbl AUTO_INCREMENT=310;

Friday, September 9, 2016

The proper way of doing SQL "INSERT ON DUPLICATE UPDATE"

$sql = 'INSERT INTO table_name (column1, column2, column3, column4) VALUES (?, ?, ?, ?) ON DUPLICATE KEY UPDATE column1=VALUES(column1), column2=VALUES(column2)';

Monday, September 5, 2016

Sunday, September 4, 2016

Cutting video without changing quality using ffmpeg

for example cut 5 min video from 10min  to 15 (10+5)
ffmpeg -i D:\videofile.mp4 -ss 00:10:00 -t 00:05:00 -vcodec copy -acodec copy D:\videofileSmall.mp4

Wednesday, August 31, 2016

Sunday, April 3, 2016

VT-x/AMD-V hardware acceleration is not available on your system. Your 64-bit guest will fail to detect a 64-bit CPU and will not be able to boot.

Okey, here solution, that worked for my Windows 10 on AMD 64bit processor.
I go to bios->advanced->cpu -> Virtualization set enabled.

vagrant @ rb_sysopen - /dev/null (Errno::EISDIR)

quick fix:
if you have error while using  vagrant up
@ rb_sysopen - /dev/null (Errno::EISDIR)

Just go to C:/dev and delete null folder or rename it to smth else if worry.

Tips for (My)SQL tables, attributes, indexes

First of all if you have hard time to get what is better to do - read official docs about attribute sizes, indexes and so on.
Second if you still can't make decision install wordpress or other popular cms and check how they do some stuff like indexes.

As I have installed wordpress and checked all the stuff there were I come to conclusion:
Collation:
utf8mb4_unicode_ci
index tip1: make indexes on every attribute you are going to make where statements, max or group by. And please read it on indexes official  documentation
index tip2: if you think you need uniq index and it is only one - make it primary.


Simple many to many relations
article (id, date, text); id primary. Anything else you need to use where, max, group by etc - simple index
tags (id, name, slug), id - primary, name and slug simple index (as we need them a lot we will use them from indexes data and will not need to get data from the disc
article_tags (article_id, tag_id). Primary index on 2 columns (article_id, tag_id) and simple index on tag_id.

Remember when using index on more than 1 column the order is very important. Read more in official docs. But basically if you have index on columns a, b, c you can benefit from index only when searching a, or a + b or a+b+c. If you search on b or c or b+c value it will not use index.
Learn more about this as usually at official docs http://dev.mysql.com/doc/refman/5.7/en/multiple-column-indexes.html


If you don't need -number (for example -12345 with minus sign), on number types make them unsigned

Thursday, March 10, 2016

export database without phpmyadmin

Hi! sometimes you need to get database, but you haven't phpmyadmin/sqladminer nor or you just want to do it faster.
So you can do it loggining via ssh and use some mysql command or create script with

<?php
exec('mysqldump --user=MYUSERNAME --password=MYPASSWORD --host=localhost MYDATABASENAME > backup.sql');
exit('done');

and run it

Thursday, March 3, 2016

Extending multiple controllers in Code Igniter

Hi! If you are forced to use old framework like codeigniter, and want to have multiple base controllers and then extend them here a trick you can do:

1) check the value of
$config['subclass_prefix'] = 'MY_';
in application\config\config.php
By default it is MY_ ; but nvm check to make sure.
2) create php file called MY_Controller.php (or your %prefix%Controller.php inside application\core folder
3) In this file (MY_Controller.php) you can create any amount of base controllers that extends CI_Controller, and then in simple controllers extend these base controllers
<?phpif (!defined('BASEPATH')) {    exit('No direct script access allowed');}

class Admin_Controller extends CI_Controller{    }
class Public_Controller extends CI_Controller{
}

Saturday, January 23, 2016

you have to start mysqld with --tc-heuristic-recover switch to commit or rollback pending transaction

PROBLEM

Your mysql don't work saying smth like:
you have to start mysqld with --tc-heuristic-recover switch to commit or rollback pending transaction
or
No connection could be made because the target machine actively refused it.

You have probably shutdown pc when there were some mysql transactions or whatever it was doing.


SOLUTION


Go to mysql bin folder open cmd and type:

mysqld --tc-heuristic-recover=ROLLBACK
Don't forget to restart mysql after this command finished