Connection failed: could not find driver

PHP development


Lesson 11

Database

When we want to store data, for instance from a form, we can do so in different ways. The simplest way is simply write it to a file:

Example


$data = $_GET;

if( !empty($data) ){
    $file = $basedir . 'data/datafile.txt';
    $string = '------------'.PHP_EOL;
    
    foreach($data as $key => $value){
        $string .= "$key = $value".PHP_EOL;
    }
    
    $wf = fopen($file, 'a');
    fwrite($wf, $string);
    fclose($wf);
}

The problem with that is that when you want to use that data, for instance to look up a contact, you have to open that file manually and look it up.

A better way then the one above is to write it to a comma separate file (.csv) that you can open with excell. Then searching through it is a little more friendly.


  if( !empty($data) ){
      $file = $basedir . 'data/datafile.csv';
      $string = '';
      foreach($data as $key => $value){
          $string .= $value . ';';
      }
      $string .= PHP_EOL;
      
      $wf = fopen($file, 'a');
      fwrite($wf, $string);
      fclose($wf);
  }

However if you want to retrieve data from these files to use it in your website, it would be very difficult to do so, especially when the dataset becomes bigger, with multiple files. That is why we use a database to store and retrieve data. It uses a special query language, SQL, that can be used to retrieve xactly the data that you want.

The Database engines that we can use with PHP are MySQL, PostgreSQL, SQLight, MsSQL to name some. We will be using MariaDB.

MariaDB

MariaDb is fork of MySQL, meaning it shared the same basic source. We can say it is just a different flavour of MySQL and in PHP we can use exaclty the same commands that we would use for the MySQL engine.

to find out if maria is installed on the server you can use the following command in your terminal:


  sudo dpkg --get-selections | grep mariadb
  
  libmariadb3:amd64				install
  mariadb-client-10.6				install
  mariadb-client-core-10.6			install
  mariadb-common					install
  mariadb-server					install
  mariadb-server-10.6				install
  mariadb-server-core-10.6			install
  

if nothing is listed it means MariaDB needs to be installed:


  sudo apt update
  
  sudo apt install mariadb-server
  sudo mysql_secure_installation

After installation, when the engine is running you are able to login into MariaDB as root:


  sudo mariadb
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 43
Server version: 10.6.18-MariaDB-0ubuntu0.22.04.1 Ubuntu 22.04

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>
  

As you see it entered root into the MariaDB shell. From there you can do all kind of things like adding databases, adding columns to databases query database etc. Most of these tasks however we will do with a graphical mysql database manager. However, it is wise to set up 1 user first that will be able to do all these task or else you need to login as root, which is not recommended since root can only be accessed when you are root on the server.

So, now we will create user which we will call 'superuser' and will be able to do everything on all databases. 'superuser' also needs a password. we will add that to our password manager 'Bitwarden' so we can always retrieve it.


  GRANT ALL ON *.* TO 'superuser'@'localhost' IDENTIFIED BY 'password' WITH GRANT OPTION;
  

Now that we have created a super user, we can safely exit the mariadb shell.


  MariaDB [(none)]> exit
  Bye
  
  

Sequel Ace

Now we want to be able to use our database engine with a graphical interface. There are many software packages available for macosx, some pai and some free. We will use Sequel Ace because it is free but most important it works well.

You can install Sequel Ace from the App Store

After installation we start Sequal Ace from our Apps directory. When is starts up it looks likes this: