• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
Locked
0

Connection Script to MariaDB using PDO PHP 7 fails

Participant ,
Feb 10, 2018 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.

 

TOPICS
Code , Error

Views

14.1K

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
community guidelines

correct answers 1 Correct answer

Community Expert , Feb 11, 2018 Feb 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'?

Votes

Translate

Translate
Community Expert ,
Feb 11, 2018 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'?

Wappler, the only real Dreamweaver alternative.

Votes

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
community guidelines
Participant ,
Feb 12, 2018 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

Votes

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
community guidelines
Community Expert ,
Feb 12, 2018 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();

  }

?>

Wappler, the only real Dreamweaver alternative.

Votes

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
community guidelines
Participant ,
Feb 13, 2018 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.

Votes

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
community guidelines
Community Expert ,
Feb 13, 2018 Feb 13, 2018

Copy link to clipboard

Copied

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

Wappler, the only real Dreamweaver alternative.

Votes

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
community guidelines
New Here ,
Apr 12, 2018 Apr 12, 2018

Copy link to clipboard

Copied

LATEST

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.

Votes

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
community guidelines