CREATE COPY OF THE TABLE WITH INDEXES

CREATE TABLE newtable LIKE oldtable;

MySQL Iterate method WHILE

CREATE PROCEDURE example1()
label1:BEGIN
DECLARE rowCount INT DEFAULT 0;
DECLARE i INT DEFAULT 0;
 
SELECT COUNT(*) INTO rowCount FROM my_table;
 
WHILE (i <= rowCount) DO
 
SELECT i;
 
SET i = i+1000;
END WHILE;
END;

MySQL Iterate method REPEAT

The difference with WHILE is that it execute statement before checking the condition. As a result, it iterates one more time than WHILE does.

CREATE PROCEDURE example2()
label1:BEGIN
DECLARE rowCount INT DEFAULT 0;
DECLARE i INT DEFAULT 0;
 
SELECT COUNT(*) INTO rowCount FROM my_table;
 
REPEAT 
 
SELECT i;
 
SET i = i+1000;
UNTIL i > rowCount
END REPEAT;
END;

Create MySQL function

In MySQL, function is different than procedure by returning the value and can be called within SELECT statement.

DETERMINISTIC means that the function always produces same results for the same input parameters.

CREATE FUNCTION `Sample`(parameter VARCHAR(10)) RETURNS TINYINT(3) UNSIGNED
    DETERMINISTIC
BEGIN
    DECLARE myfunctionID TINYINT UNSIGNED;
 
    IF parameter = 'sample1' THEN
		SET returnID = 1;
    ELSEIF parameter = 'sample2' THEN
		SET returnID = 2;
    ELSEIF parameter = 'sample3' THEN
		SET returnID = 3;
        END IF;
    END IF;
 
 RETURN myfunctionID;
END

Add SPF record for your email relay server to avoid being counted as a spam

Step 1. Add SPF record to your DNS server

Create TXT record type and add the following text without quotes “”

@ TXT "v=spf1 a mx include:myrelay.com ~all"

Step 2. Check if SPF record is added correctly

username@laptop:/home/username# dig myhostname.com TXT
 
 <<>> DiG 9.10.3-P4-Ubuntu <<>> myhostname.com TXT
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 62028
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
 
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4000
;; QUESTION SECTION:
;myhostname.com.		IN	TXT
 
;; ANSWER SECTION:
myhostname.com.	86400	IN	TXT	"v=spf1 a mx include:myrelay.com ~all"
 
;; Query time: 7 msec
;; SERVER: 127.0.1.1#53(127.0.1.1)
;; WHEN: Wed Nov 22 13:36:40 GMT 2017
;; MSG SIZE  rcvd: 104

Step 3. Test it by sending email to your gmail account, then go the receiver’s mailbox and check email source header

Received-SPF: pass (google.com: domain of www-data@myhostname.com designates 24.33.20.159 as permitted sender) client-ip=24.33.20.159;
Authentication-Results: mx.google.com;
       spf=pass (google.com: domain of www-data@myhostname.com designates 24.33.20.159 as permitted sender) smtp.mailfrom=www-data@myhostname.com

Redirect va htaccess non-www to www and keep http and https protocols

RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTPS}s on(s)|offs()
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R]

Mysql number of tables in DB

SELECT COUNT(*) FROM information_schema.TABLES WHERE table_schema = 'databasename';

Mysql create utf-8 database

Former practice:

CREATE DATABASE mydatabase CHARACTER SET utf8 COLLATE utf8_general_ci;

Current practice:

CREATE DATABASE mydb CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

Postfix localhost IP address only bind configuration

Open /etc/postfix/main.cf file:

bash# vi /etc/postfix/main.cf

Append / modify line as follows to bind to localhost (127.0.0.1) only:

inet_interfaces = 127.0.0.1

Type the following to restart Postfix:

bash# /etc/init.d/postfix restart

function memory_get_usage

 
<?php
 
function memoryChecker($variable) {
echo 'in function: '.memory_get_usage()."\n";
}
 
echo 'before function: '.memory_get_usage()."\n";
 
$var = "hello world\n";
 
memoryChecker($var);