Error getting database connection: could not find driver

My website URL is:
http://sflondon.onlinewebshop.net/SideTrekked.php

Once on that page, scroll to the bottom and click on the blue link that says “List of all past contributions to SideTrekked”. On the resulting form, choose any of the three radio buttons and click on the Display Report button.

What I’m seeing is:
Error getting database connection: could not find driver

I’m using this software:
Your MySQL installation.

Additional information:

This is the PDO connection code (with the password obscured but I assure you I pasted in the HOSTING password from my accounts list):
<?php

$mysql_hostname = "sql212.epizy.com";
$mysql_dbname = "epiz_23494076_sflondon";
$mysql_username = "epiz_23494076";
$mysql_password = "********"; /* hosting account password!!! */

try { 
    $PDO_arg = "'mysql:host=".$mysql_hostname.";dbname=".$mysql_dbname."','".$mysql_username."','".$mysql_password."'";
  //  echo "PDO_arg: " . $PDO_arg . "\n";
    $db = new PDO($PDO_arg);
	$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
	$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
	}
catch (PDOException $excp) {
	echo "\nError getting database connection: " . $excp->getMessage() . "\n";
	}   

?>

I’m on a free account, not a premium one but I get the impression that I should still be able to access MySQL in the way I am doing, as long as my hostname, dbname, username and password are correct, which they appear to be. I’m also under the impression that connecting via PDO is okay, even on a free account. What am I missing or doing wrong?

You should remove the apexes from the MySQL PDO connection URL. If not, it throws an error during the connection.

Could you please clarify? I have no idea what an “apex” is in this context or how to remove one. Is that in the code for the PDO connection that I quoted? Or a setting in MySQL or PHP?

I see your PDO connection string looks like this:

$PDO_arg = "'mysql:host="

Removing the single quotes from the start of that line probably fixes the issue. It would explain the error could not find driver when you tell PDO to look for the driver 'mysql rather than mysql.

Oh, I see! I think you must have meant to say apostrophes, not apexes. Perhaps English is a second language for you because I’ve never heard of that character being called an “apex” by a native English-speaker…

Now that I know what you mean, let me give it a try. I’ll report back either way. :slight_smile:

I took the connection code back to its simplest form with no variables or concatenation and also removed the apostrophes.

$db = new PDO(mysql:host=sql212.epizy.com;dbname=epiz_23494076_sflondon, epiz_23494076, *******);

This version is not even valid PHP code due to the equals sign in the middle of the first two parameters.
If I restore the apostrophes to where they (probably) should be, I get this:

   $db = new PDO('mysql:host=sql212.epizy.com;dbname=epiz_23494076_sflondon', 'epiz_23494076', '*******');

That version is valid PHP code but gives me this error message:
Error getting database connection: SQLSTATE[HY000] [2002] Connection refused

It appears that my attempt to generate the connection string with concatenation and variables was not quite correct, causing the driver error. Removing the apostrophes does not help though. There must be a different problem.

Therefore, can you confirm that it is possible for someone with a free account to access MySQL via PDO in this way from my own programs running on your server? If it IS possible, what am I doing wrong now that the connection string is completely clean?

I’m still really eager to resolve this point. Could someone kindly answer my question: Is it possible for me to write my own programs, running on the server, that access my MySQL database if I have a free account here?

If it is possible, that would be great but I’d really like to use PDO if that’s possible. Can someone please confirm if I can use PDO from my own account?

And lastly, if I can access MySQL from PDO on a free account, what’s wrong with my connection string as shown above?

My project is about to be at a standstill unless someone can answer these questions.

You should be able to connect to your database from the PHP code on your free hosting account. You can use both the mysqli or PDO drivers to do so. And the connection string you’ve shown above looks correct.

So I checked your website (http://sflondon.onlinewebshop.net/SideTrekkedContributions.php) again to see what the full error message was to see what could be going on. But I noticed that the file path from where the error was being generated does not match the directory structure on our servers. I also checked the IP address of the domain, and saw that it wasn’t ours.

It looks like the domain sflondon.onlinewebshop.net is not hosted on InfinityFree, but somewhere else. And connecting to a database from outside your InfinityFree account is not possible.

So in order to make your code work, you will need to move it to your InfinityFree account first. Note that you may not be able to use the same domain name though, because onlinewebshop.net looks like a free subdomain service, which you may not be able to bring to other providers.

Thank you VERY MUCH, Admin! That helps clear up the picture. NOW I finally understand what the problem is and can make the necessary changes at my end :slight_smile:

Thanks again!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.