Highlighted

Connection Script to MariaDB using PDO PHP 7 fails

Participant ,
Feb 10, 2018

Copy link to clipboard

Copied

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.

 

Adobe Community Professional
Correct answer by BenPleysier | Adobe Community Professional

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'?

TOPICS
Code, Error

Views

10.7K

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Connection Script to MariaDB using PDO PHP 7 fails

Participant ,
Feb 10, 2018

Copy link to clipboard

Copied

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.

 

Adobe Community Professional
Correct answer by BenPleysier | Adobe Community Professional

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'?

TOPICS
Code, Error

Views

10.7K

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Feb 10, 2018 0
Adobe Community Professional ,
Feb 11, 2018

Copy link to clipboard

Copied

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'?


Ben

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Feb 11, 2018 1
Participant ,
Feb 12, 2018

Copy link to clipboard

Copied

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.

php_info1.png

php_info2.png

ini_file.png

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Feb 12, 2018 0
Adobe Community Professional ,
Feb 12, 2018

Copy link to clipboard

Copied

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();

  }

?>


Ben

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Feb 12, 2018 1
Participant ,
Feb 13, 2018

Copy link to clipboard

Copied

Thanks Ben for all your assistance.

As you saw, I activated the extension then it (finally) struck me I had not granted appropriate privileges to the users for this particular database.  Jumped on my SQLyog, went to User Management took care of the privilege issue.  Connection script worked great.

Sorry to have not gotten back to you sooner.  Since I'm on other side of world, little delay on my part.

Again thanks for all the help and I will keep the other connection script you provided as a good backup resource.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Feb 13, 2018 0
Adobe Community Professional ,
Feb 13, 2018

Copy link to clipboard

Copied

Thanks for the feedback and very glad you managed to resolve the issue.


Ben

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Feb 13, 2018 0
olivedev LATEST
New Here ,
Apr 12, 2018

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 12, 2018 0