I am trying to connect database in sql server 2014 with php as follows:
First I have this script in conexaosql.php:
class Conexao
{
private static $connection;
private function __construct(){}
public static function getConnection() {
$pdoConfig = DB_DRIVER . ":". "Server=" . DB_HOST . ";";
$pdoConfig .= "Database=".DB_NAME.";";
try {
if(!isset($connection)){
$connection = new PDO($pdoConfig, DB_USER, DB_PASSWORD);
$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
return $connection;
} catch (PDOException $e) {
$mensagem = "Drivers disponiveis: " . implode(",", PDO::getAvailableDrivers());
$mensagem .= "nErro: " . $e->getMessage();
throw new Exception($mensagem);
}
}
}
Then I call this script and I have the following code:
define('DB_HOST' , "xxxx");
define('DB_USER' , "xxxx");
define('DB_PASSWORD' , "xxxx");
define('DB_NAME' , "xxxx");
define('DB_DRIVER' , "sqlsrv");
require ("conexaosql.php");
try{
$Conexao = Conexao::getConnection();
$query = $Conexao->query("SELECT Pago FROM UTE02.dbo.Recibos");
$produtos = $query->fetchAll();
}catch(Exception $e){
echo $e->getMessage();
exit;
}
I get the following error when I run the code:
mysql,sqlite Erro: could not find driver
I’m using php 8.1 and apache. I leave the link with images from phpinfo()
2
Answers
I solved my problem by running the following commands:
You need to install the PDO_SQLSRV driver to access the database. You have only installed support for MySQL and SQLite.
If you run a Linux server, you have to install and use ODBC.