Skip to main content
Inspiring
February 11, 2018
Answered

Connection Script to MariaDB using PDO PHP 7 fails

  • February 11, 2018
  • 2 replies
  • 14710 views

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.

 

This topic has been closed for replies.
Correct answer BenPleysier

1. Have you activated 'extension=php_pdo_mysql.dll' ?

2. Is 'localhost' recognised or do you need an IP-address like '127.0.0.1'?

2 replies

Participant
April 12, 2018

There might be something wrong with your php script. You can follow this tutorial to get the correct script and know how to correctly make pdo connection in PHP.

BenPleysier
Community Expert
BenPleysierCommunity ExpertCorrect answer
Community Expert
February 11, 2018

1. Have you activated 'extension=php_pdo_mysql.dll' ?

2. Is 'localhost' recognised or do you need an IP-address like '127.0.0.1'?

Wappler is the DMXzone-made Dreamweaver replacement and includes the best of their powerful extensions, as well as much more!
RR456Author
Inspiring
February 12, 2018

Thanks for the reply Ben.

Something must be side-ways on the server I spun-up at GCE since my connection script is still not working and I have very limited skills as a System Admin.

I've included below 3 screen shots that I believe show I have PDO enabled and the extension=php_pdo_mysql.dll activated.  In addition, I changed the script from 'localhost' to '127.0.0.1', restarted Apache from the command line (pretty sure) limited "flight time" on a command line, still no connection.

Any other suggestions would be appreciated; but, like I say maybe it's not the script but the server set-up that's not letting PDO interface with MariaDB.

BenPleysier
Community Expert
Community Expert
February 13, 2018

Rather than using your script to connect, try this

<?php

$server = "localhost";

$user = "mswaxa15_psread";

$pwd = "j3&uG39#bR2m";

try {

  $connection = new PDO("mysql:host=$server;dbname=mswaxa15_mqzmkt", $user, $pwd);

  // PDO can throw exceptions rather than Fatal errors, so let's change the error mode to exception

  $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

  echo "connection successful";

  }

catch(PDOException $e)

  {

  echo "Connection failed: " . $e->getMessage();

  }

?>

Wappler is the DMXzone-made Dreamweaver replacement and includes the best of their powerful extensions, as well as much more!