I am working with the SQL Server 2008 and php pdo. I am trying to connect from php to sql server 2008.
my php version is:5.5.12
I am getting error :: could not find driver
what I tried is to connect to sql server 2008 database from php like:
<?php
$dbhost = "localhost";
$dbname = "xxxxxx";
$dbuser = "xx";
$dbpass = "";
try {
$con = new PDO("sqlsrv:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
} catch (PDOException $e) {
echo $e->getMessage();
}
?>
I also surfed a lot but did not found the solution.
my extensions in php.ini
extension=php_sqlsrv_55_ts.dll
extension=php_pdo_sqlsrv_55_ts.dll
location where i put the dll is C:\wamp\bin\php\php5.5.12\ext
Related
I want to connect to SQL Server at another pc with php, when i try to connect it says
Connection error: could not find driver
What I have tried is add extension in php.ini.
;extension=php_sqlsrv_56_ts.dll
extension=php_pdo_sqlsrv_72_ts_x64.dll
extension=php_sqlsrv_72_ts_x64.dll
my PHP version is: 7.2.3
SQL SERVER: SQL Server 2014
OS PC: Windows Server 2012 R2 (another pc)
here's my code:
<?php
$host = "192.168.3.126";
$db_name = "dbo";
$username = "david";
$password = "david";
try {
//$con = new PDO("mysql:host={$host};dbname={$db_name}", $username, $password);
$db = new PDO("sqlsrv:Server={$host};Database={$db_name}", $username, $password);
}
catch(PDOException $exception){ echo "Connection error: " . $exception->getMessage();}
?>
NB: I have download Microsoft Drivers 5.6 for PHP for SQL Server at here, I have download install the ODBC Driver 17 at my own pc
I have tried this reference but it doesn't work PHP Sql Server PDOException:could not find driver
I had the php version 5 but now i upgraded to php 7 and i am getting problems that a didn't had with the older version.
One that i'm still trying to solve is this:
Could not connect to the database mydatabase :could not find driver (this appear when i run my script).
I tryed to open phpmyadmin and also appeared an error:
The mysqli extension is missing. Please check your PHP configuration.
After a lot of search i still can find a solution, is this related to the version of my mysql on xampp? It need to be upgraded too?
Apache version: 2.4
mysqlnd 5.0.12
********************** EDIT*****************
Here is the code that makes the connection to database:
function connection()
{
$host = 'localhost';
$dbname = 'mydatabase';
$username = 'root';
$password = '';
try {
$conn = new PDO("mysql:host=$host;dbname=$dbname;", $username, $password);
$conn->exec("SET CHARACTER SET utf8");
// echo "Connected to $dbname at $host successfully.";
}
catch(PDOException $pe) {
die("Could not connect to the database $dbname :" . $pe->getMessage());
echo "Line: " . __LINE__;
}
return $conn;
} //connection
Either install the mysqli driver on your server or use PDO, if that's available. Use this code to find out more information about the installed drivers:
<?php phpinfo() ?>
Go to your php.ini file and uncomment this line
extension=mysqli
Then restart your local server
This was how i solved the problem:
1º rename php.ini-developer to php.ini
2º add extensions:
extension_dir = "D:\Programs\xampp\php\ext"
extension=php_mysqli.dll
extension=pdo_mysql
extension=mbstring
extension=php_mbstring.dll
Working like a charm now!
My PC has Installed MS SQL Server 2008 R2 with installed Database, Then I installed XAMPP and made another port for Apache to make it localhost:8080 and listen to 4433. Then Installed OBDC, Tried to connect using
try {
$host = 'LOCAL';
$user = '****';
$pass = '****';
$dsn = "sqlsrv:Server=$host;Database=$db;";
$opt = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
];
$pdo = new PDO($dsn, $user, $pass, $opt);
} catch (PDOException $e) {
echo "No connection: " . $e->getMessage();
exit;
}
But I keep getting
No connection: SQLSTATE[08001]: [Microsoft][ODBC Driver 13 for SQL Server]Named Pipes Provider: Could not open a connection to SQL Server [53].
Used extension=php_pdo_sqlsrv_71_ts_x86.dll
I've shutdown Windows Firewall, Enabled Pipe Connection in MS SQL 2008 Configuration and Internet Option NetBIOS Enabeled
$host shouldn't be "LOCAL" but "localhost".
If you are running SQL server on a custom port on your local machine, you can specify the port by appending it with a comma to the host; applied to your code snippet:
$host = "localhost";
// or, with a custom port
$host = "localhost,12345"
Source: http://php.net/manual/en/ref.pdo-sqlsrv.connection.php#refsect1-ref.pdo-sqlsrv.connection-examples
if you are using a local instance, the server string needs a double backslash:
<?php
$conn = new PDO('sqlsrv:Server=localhost\\SQLEXPRESS;Database=MyDatabase', 'MyUsername', 'MyPassword');
?>
See original provided answer by Daniel Klein:
https://www.php.net/manual/en/ref.pdo-sqlsrv.connection.php#121301
I'm using PHP Version 5.6.25 that was installed via WAMPP. I'm having issues connecting to my database on mySQL DB:
$server = 'jdbc:sqlserver://DB-1\POWERPIVOT;databaseName=SBV_Foldio';
$user = 'sa';
$pass = 'host';
I had check PDO extension and already install
if (!defined('PDO::ATTR_DRIVER_NAME')) {
echo 'PDO is unavailable<br/>';
}
elseif (defined('PDO::ATTR_DRIVER_NAME')) {
echo 'PDO is available<br/>';
}
PDO is available
but when i run connection
try {
$dbh = new PDO($server, $user, $pass);
$dbh = null;
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
}
it failed
Error!: could not find driver
The problem is that you do not have the PDO_ODBC module installed right now. PDO seems to be properly configured & installed. See http://php.net/manual/en/ref.pdo-odbc.php for more info.
You are obviously running windows, so:
On Windows, php_pdo_odbc.dll has to be enabled as extension in php.ini. It is linked against the Windows ODBC Driver Manager so that PHP can connect to any database cataloged as a System DSN, and is the recommended driver for connecting to Microsoft SQL Server databases.
Set up:
Wamp Server
PHP 5.5.12
Apache 2.4.9
MS SQL Server 2012
Already completed:
Installed (unofficial) php_sqlsrv_55_ts and php_pdo_sqlsrv_55_ts AND confirmed they are working via phpinfo() sqlsrv_link
I have made sure that TCP/IP is enabled in SQL Configuration Manager
I have tested credentials in SQL Management Studio AND via ODBC in Administrrative Tools
IIS is disabled
Additionally, I did have trouble with SQL Reporting Service taking port 80. This was giving me a problem with Apache, so I directed SQL Reporting Service to use port 8081.
<?php
$server = "computer_name\MSSQLSERVER";
$user = "sa";
$pass = "password";
$db = "pcm";
$connInfo = array("Database"=>$db, "UID"=>$user, "PWD"=>$pass);
$conn = sqlsrv_connect($server, $connInfo) or die( print_r( sqlsrv_errors(), true));
?>
If you are using the PDO extension, you will have to use a PDO connection instead of the sqlsrv_connect() function. Microsoft doesn't support the UID or PWD keys that you are providing when trying to connect via the PDO extension. Try this instead:
$conn = new PDO('sqlsrv:Server = ' . $server . '; Database = ' . $db, $user, $pass);
http://php.net/manual/en/ref.pdo-sqlsrv.connection.php