0

I'm trying to connect Access Database to my website via PHP, Using PDO . But, I'm getting this strange message

Data source name not found and no default driver specified.

I'm fully aware that there are multiple questions and answers regarding the same on Stack Over Flow but none of them worked for me.

this is my code to connect to my Access database .


<?php

$dbname = 'Sea_Service_Certificates.accdb';

if(!file_exists($dbname)){

    echo "not found";

}

$inp = $_POST["input"];

echo $inp . "<br>";   

try {

    $db = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=$dbname;Uid=; Pwd=;");
    
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $sql = "Select *from Certificate Details where PassportNumber = 
    "+$inp+";";

    $result = $db->query($sql);

    $row = $result->fetch();

    $name = $row["Name"];

    echo "<h1><a href='index.php'>$name</a></h1>";

} catch (PDOException $e) {

    echo "Error : " . $e->getMessage()."<br>";

}

?>

The code works like a charm in local host but when I upload to my web host which is BIGROCKS it doesn't work and throws the error.

I did every possible solution on the internet including customizing my php.ini file .

Thanks in advance.

ADyson
  • 57,178
  • 14
  • 51
  • 63
BHN GROUP
  • 31
  • 1
  • 6
  • https://stackoverflow.com/help/formatting - please look at that and then make your code readable, thanks – ADyson Jul 12 '21 at 09:01
  • Does this answer your question? [Why am I getting "Data source name not found and no default driver specified" and how do I fix it?](https://stackoverflow.com/questions/58571740/why-am-i-getting-data-source-name-not-found-and-no-default-driver-specified-an) – Erik A Jul 12 '21 at 09:03
  • 1
    Contact your hoster to install Access for you. They will likely say something like "No, no way, we're using Linux so we can't do it, and even if we were not, we wouldn't since Access is explicitly not supported for use in web services", and that'll be that. Then either consider buying a VPS with Windows and creating a terrible mess for yourself, or not using Access for web services. – Erik A Jul 12 '21 at 09:05
  • Access is a terrible choice as a backend for a website, it's meant for desktop use with a single user or very small number of users. It will not scale well and support on Linux (which is probably what your hosting uses) isn't great, you're stuck with ODBC - which seems to be what's missing here. Use mysql, postgresql or even sqlite instead, they are all far better options – ADyson Jul 12 '21 at 09:06
  • @ErikA thank you for the information , I have one more question that when I run ````print_r(PDO::getAvailableDrivers());```` on hoster side it shows me he has ODBC driver. So they might already have Access – BHN GROUP Jul 12 '21 at 09:09
  • @BHNGROUP That lists the available PDO drivers, not the available ODBC drivers. See [php.net](https://www.php.net/manual/en/function.odbc-data-source.php) for how to list the available ODBC drivers. – Erik A Jul 12 '21 at 09:26
  • @ErikA Thank you so much for the information. will check some alternate options . – BHN GROUP Jul 12 '21 at 09:28

0 Answers0