6 Replies Latest reply on Apr 12, 2018 4:50 AM by olivedev

    Connection Script to MariaDB using PDO PHP 7 fails

    RR456 Level 1

      I have a site on a production server with a hosting company that has connected to our MariaDB for a couple years with no problem using our connection script in PDO version 7.x PHP.

       

      I'm looking at moving our sites to Google Compute Engine on Google Cloud Platform  so I'm running a trial VM. I've spun-up a debian-9-stretch and installed Apache, MariaDB and PHP 7.x.

       

      My connection script fails to connect to my database thus getting the error message Cannot connect to database.  I have checked the usernames and passwords and the database name for errors and there are none.  The read and write usernames and their encrypted passwords are in database MySQL table user and were placed in that table using the command line.  The connection script is:

       

      <?php

      function dbConnect($usertype, $connectionType = 'pdo') {

      $host = 'localhost';

      $db = 'mswaxa15_mqzmkt';

      if ($usertype == 'read') {

        $user = 'mswaxa15_psread';

        $pwd = 'j3&uG39#bR2m';

      } elseif ($usertype == 'write') {

        $user = 'mswaxa15_pswrite';

        $pwd = 'Ne#6B?uQx9#2k';

      } else {

        exit('Unrecognized user');

      }

      if ($connectionType == 'mysqli') {

        $conn = new mysqli($host, $user, $pwd, $db);

        if ($conn->connect_error) {

         exit($conn->connect_error);

        }

        return $conn;

      } else {

        try {

         return new PDO("mysql:host=$host;dbname=$db", $user, $pwd);

        } catch (PDOException $e) {

         echo 'Cannot connect to database';

      exit;

        }

      }

      }

      Any help getting this connection script to work would be greatly appreciated.  Thank you.