Categories
MySql

What is the easiest way to run Mysql on a Mac ?

The easiest way to run MySQL on a Mac is to install MAMP. The advantage is that it does not conflict with valet which is another powerful tool for local development specially with Laravel. If you do not want to face the same issue as the lost of your mysql password after installing with homebrew, MAMP is the my fastest solution

See StackOverflow issue : MacOSX homebrew mysql root password

Categories
PHP

What is a palindrome ?

What is a palindrome ? Find the definition below from the online oxford

a word, phrase, or sequence that reads the same backward as forward, e.g., madam or nurses run.

In the example below only a word palindrome could be identified. Additional conditions would be necessary to analyze phrase or sequences

function isPalindrome($str): bool
{
    $str_length = strlen($str);
    $mid = round($str_length / 2);
    $last_character = $str_length - 1;

    for ($i = 0; $i <= $mid; $i++) {
        if ($str[$i] != $str[$last_character]) {
            return false;
        }
        $last_character--;
    }
    return true;
}

var_dump( isPalindrome('madam'));
var_dump( isPalindrome('nursesrun'));
var_dump( isPalindrome('davad'));
var_dump( isPalindrome('avaava'));

What is the run time complexity ?

It sounds to be O(n/2)

What is the space complexity ?

We do not allocate memory except for few variables as $i, $mid and $last_character

O(1)

Categories
Laravel

How to execute PHP code from the command line?

Simple just type

php -a

More info about php :

Categories
Interview Interview questions

Big O notation Chart

Speed performance is the key of any successful application – use the following charts for understanding the big O nation and for upcoming FAANG interviews

source https://jarednielsen.com/big-o-notation/

https://twitter.com/jarednielsen

Also do not hesitate to prepare your interviews with this book

Categories
wordpress

How to make twitter card working with wordpress ?

First please install the plugin SmartCrawl

then configure as follow

Categories
General

Twitter Card not working ERROR: Fetching the page failed because other errors.

If you are experiencing the issue below , you can try here

https://cards-dev.twitter.com/validator

Enabling AES128 as an ssl cipher will allow the Twitterbot to connect.

See different forums confirming the solutions

https://twittercommunity.com/t/twitter-card-error-fetching-the-page-failed-because-other-errors/112895/7

If you are using cloudfare, please see configuration

See more information about cloudfare

https://developers.cloudflare.com/ssl/ssl-tls/cipher-suites

then it will work

Categories
SqLite

How to fix/open Unable to open database “xxxxxsqlite”: file is encrypted or is not a database ?

It means that your are trying to open with SQLITE rather than SQLITE 3

Categories
SqLite

How to add a new column to an existing sqlite database (sqlite3) ?

ALTER TABLE table_name ADD COLUMN column_definition;

Categories
Technical

How to change root password in Digital Ocean ?

Well it may come as a surprise when you figure that you need the root password of your droplet to do advance configuration on your server. Find below How to change the root password of your droplet in Digital Ocean ?

Categories
Postgres

How to PHP Singleton with different schema Postgres Database connection PDO ?

/**
 * Class StorageService
 *
 * @package App\Core\Services
 */
class StorageService extends \App\Core\Classes\Service
{

    private static $instance = null;
    private $storageConfigs = false;
    private $dbConnections = false;

    public $schema ;
    public $database_host ;
    public $database_port ;
    public $database_name ;
    public $database_user ;
    public $newConnection ;


    public function __construct($schema = 'api')
    {

        $dbConfigs = $this->storageConfigs()['databases'];
        
        $dbConfig = $dbConfigs[$schema];

        $this->schema = $schema;
        $this->database_host = $dbConfig['database_host'];
        $this->database_port = $dbConfig['database_port'];
        $this->database_name = $dbConfig['database_name'];
        $this->database_user = $dbConfig['database_user'];
        $database_password  = $dbConfig['database_password'];
        

        $dsn = "pgsql:host={$this->database_host};port={$this->database_port};dbname={$this->database_name};";
        $pdoOpts =
            [
                \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
                \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC
            ];

        try {
            $this->newConnection = new \PDO($dsn, $this->database_user, $database_password, $pdoOpts);

        } catch (\Throwable $e) {

            /**
             * Add Exception error
             *
             * @date July 15th 2021
             * @author David Raleche
             */
            $error = 'MAPI CANT CONNECT DATABASE schema'.$schema
                .' DSN: '.$dsn
                .' USER: '.$this->database_user
                .' PASSWORD'.$database_password
                .' '.(string) $pdoOpts
                . '  ' .$e->getMessage();
            HelperService::logger('critical', $error);
            throw new \Exception('Database connection issue '.$e->getMessage(), 500);
        }



    }


    /**
     * Storage Configs
     *
     * @return bool|mixed
     */
    private function storageConfigs()
    {
        if ($this->storageConfigs === false) {
            $this->storageConfigs = yaml_parse_file(__DIR__ . '/../../config/storage.yaml');
        }
        return $this->storageConfigs;
    }

    /**
     * Get Db connection
     *
     * @param string $schema
     * @param bool $allowShared
     *
     * @return bool|\PDO
     */
    
    public function getConnection() : \PDO
    {
        return $this->newConnection;
    }


    public static function getInstance($schema)
    {
        if(!self::$instance )
        {
            self::$instance = new StorageService($schema);
        }

        if ( self::$instance->schema !== $schema)
        {
            self::$instance = new StorageService($schema);
        }

        return self::$instance;
    }