PDO gives error "could not find driver" - php

<?
$dsn = 'mysqli:dbname=websiteusers;host=localhost';
$user = 'root';
$password = '123';
try {
$dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
echo 'Подключение не удалось: ' . $e->getMessage();
}
?>
It gives error could not find driver.
What can i do?

As far as I know and the php docs for PDO are saying is that PDO doesn't support mysqli since there is an own class/adapter handling mysqli connections.
Supported PDO Drivers
MySQLi

The driver is called mysql, not mysqli.

Perhaps the PDO driver for mysql is not enabled on your server. Check your php.ini file for the following line:
;extension=php_pdo_mysql.dll

Related

Connect to remote Ms-SQL server using PDO [duplicate]

I am trying to connect to an existing SQL Server database using PDO with the drivers provided by Microsoft.
I have seen examples using odbc, dblib, mssql, etc., however I believe the connection string with these drivers should use 'sqlsrv'?
Are there any good examples of how to properly do this? If I should be doing this via some other method please let me know. Thanks!
Well that's the best part about PDOs is that it's pretty easy to access any database. Provided you have installed those drivers, you should be able to just do:
$db = new PDO("sqlsrv:Server=YouAddress;Database=YourDatabase", "Username", "Password");
Mind you that in my experience and also of other (PHP - Why is new SQLSRV driver slower than the old mssql driver?) that using PDO_SQLSRV is way slower than through PDO_ODBC.
If you want to use the faster PDO_ODBC you can use:
//use any of these or check exact MSSQL ODBC drivername in "ODBC Data Source Administrator"
$mssqldriver = '{SQL Server}';
$mssqldriver = '{SQL Server Native Client 11.0}';
$mssqldriver = '{ODBC Driver 11 for SQL Server}';
$hostname='127.0.0.1';
$dbname='test';
$username='user';
$password='pw';
$dbDB = new PDO("odbc:Driver=$mssqldriver;Server=$hostname;Database=$dbname", $username, $password);
This works for me, and in this case was a remote connection:
Note: The port was IMPORTANT for me
$dsn = "sqlsrv:Server=server.dyndns.biz,1433;Database=DBNAME";
$conn = new PDO($dsn, "root", "P4sw0rd");
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$sql = "SELECT * FROM Table";
foreach ($conn->query($sql) as $row) {
print_r($row);
}
Figured this out. Pretty simple:
new PDO("sqlsrv:server=[sqlservername];Database=[sqlserverdbname]", "[username]", "[password]");
try
{
$conn = new PDO("sqlsrv:Server=$server_name;Database=$db_name;ConnectionPooling=0", "", "");
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
$e->getMessage();
}
$servername = "";
$username = "";
$password = "";
$database = "";
$port = "1433";
try {
$conn = new PDO("sqlsrv:server=$servername,$port;Database=$database;ConnectionPooling=0", $username, $password,
array(
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
)
);
} catch (PDOException $e) {
echo ("Error connecting to SQL Server: " . $e->getMessage());
}

Bootgrid with PDO database driver [duplicate]

I am trying to connect to an existing SQL Server database using PDO with the drivers provided by Microsoft.
I have seen examples using odbc, dblib, mssql, etc., however I believe the connection string with these drivers should use 'sqlsrv'?
Are there any good examples of how to properly do this? If I should be doing this via some other method please let me know. Thanks!
Well that's the best part about PDOs is that it's pretty easy to access any database. Provided you have installed those drivers, you should be able to just do:
$db = new PDO("sqlsrv:Server=YouAddress;Database=YourDatabase", "Username", "Password");
Mind you that in my experience and also of other (PHP - Why is new SQLSRV driver slower than the old mssql driver?) that using PDO_SQLSRV is way slower than through PDO_ODBC.
If you want to use the faster PDO_ODBC you can use:
//use any of these or check exact MSSQL ODBC drivername in "ODBC Data Source Administrator"
$mssqldriver = '{SQL Server}';
$mssqldriver = '{SQL Server Native Client 11.0}';
$mssqldriver = '{ODBC Driver 11 for SQL Server}';
$hostname='127.0.0.1';
$dbname='test';
$username='user';
$password='pw';
$dbDB = new PDO("odbc:Driver=$mssqldriver;Server=$hostname;Database=$dbname", $username, $password);
This works for me, and in this case was a remote connection:
Note: The port was IMPORTANT for me
$dsn = "sqlsrv:Server=server.dyndns.biz,1433;Database=DBNAME";
$conn = new PDO($dsn, "root", "P4sw0rd");
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$sql = "SELECT * FROM Table";
foreach ($conn->query($sql) as $row) {
print_r($row);
}
Figured this out. Pretty simple:
new PDO("sqlsrv:server=[sqlservername];Database=[sqlserverdbname]", "[username]", "[password]");
try
{
$conn = new PDO("sqlsrv:Server=$server_name;Database=$db_name;ConnectionPooling=0", "", "");
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
$e->getMessage();
}
$servername = "";
$username = "";
$password = "";
$database = "";
$port = "1433";
try {
$conn = new PDO("sqlsrv:server=$servername,$port;Database=$database;ConnectionPooling=0", $username, $password,
array(
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
)
);
} catch (PDOException $e) {
echo ("Error connecting to SQL Server: " . $e->getMessage());
}

PHP 5.6.x lost ability to connect to ODBC source

I have a strange problem. My server has been running PHP 5.6 for years no problem. (WinServer Std 2K8 SP2, IIS 6, MySQL 5.6 {separate server}, PHP 5.6)
We connect it to a DB2 server at our parent company. Today (2017-02-14) the ODBC connection (PDO_ODBC) started returning "could not find driver".
Excel is able to use the same ODBC connection to query the database - the ODBC connection is working.
I tried using both the PDO method and procedural method to connect. Failures in seeing the driver both ways.
From phpinfo():
ODBC Data
PDO Data
Code snippet:
$dsn = "odbc:workingODBCdsn";
$user = "xxxx";
$password = "yyyy";
$conn = null;
$results = array();
try {
$conn = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
die($e->getMessage());
}
if ($conn) {
$qry = $conn->query($sql);
if ($qry) {
$qry->setFetchMode(PDO::FETCH_ASSOC);
foreach ($qry as $row) {
$results[] = $row;
}
}
}
print "<pre>" . print_r($results, true) . "</pre>";
//ALT Method
$conn = odbc_connect($dsn, $user, $password);
$results = odbc_exec($conn, $sql);
print "<pre>" . print_r($results, true) . "</pre>";
Thanks in advance for any help.
Are you using the unixODBC or ibm_db2 (http://php.net/manual/en/ref.pdo-odbc.php)? It it recommended to use IBM DB2 Universal Database with “ibm_db2” extension. It’s faster and more efficient than using generic driver. It calls the native IBM DB2 functions with the extension.
Check out the db2_* functions from php.net IBM DB2 functions manual
Server's PHP instance switched from IIS to IISExpress. Switching it back to (full) IIS, and enduring all appropriate PDO drivers were enabled fixed the problem.

PHP PDO Keep Getting Error: Charset=UTF8 : An invalid keyword charset was specified in the dsn string

I keep getting this error : PHP PDO : Charset=UTF8 : An invalid keyword charset was specified in the dsn string.
My code is like this
function ConnectToSQLAndGetDBConnSTRVar() {
try {
$dbname = "irina";
$serverName = ".\SQLEXPRESS";
$username = "USERNAME";
$pw = "PASSWORD";
$dbh = new PDO ("sqlsrv:server=$serverName;Database=$dbname;charset=utf8","$username","$pw");
return $dbh;
}
catch (PDOException $e) {
print "Failed to get DB handle: " . $e->getMessage() . "\n";
exit;
}
}
And it doesnt matter how I write utf8.. UTF8 or UTF-8 or utf-8 none of them work for me..
So what do i do please help me..
You can find the parameters accepted in the DSN string on this page of the PHP manual.
There is no Charset parameter in DSNs for the "SQL Server" PDO driver (with the DSN prefix sqlserv:).
Bear in mind that all PDO drivers have different DSN conventions, as they are passed directly to the driver and not normalised by PDO.
There is an alternative PDO driver for SQL Server called "PDO_DBLIB", which does take charset as a DSN parameter, but it has the prefix "sybase:", "mssql:", or "dblib:", depending on compilation options.
I had same error while following the instructions from here to prevent sql injections
reading manual - it is said that prior to php 5.3.6 charset was ignored, and you can use it including options:
$dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
// Set options
$options = array(
PDO::ATTR_PERSISTENT => true,//can help to improve performance
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, //throws exceptions whenever a db error occurs
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES uft8' //>= PHP 5.3.6
);
try {
$this->conn = new PDO($dsn, $this->user, $this->pass, $options);
}
catch ( PDOException $e ) {
$this->error = $e->getMessage();
}

PDO or MSSQL_connect - in PHP 5.3

I am working on a website that runs by MYSQL and Linux Php 5.3 - and i need to work with this as well as a remote MSSQL database.
I read that PDO this is the way to connect to MSSQL.
It though seems there are both a PDO and a more familiar mssql_connect solution.
I have little to no experience with either PDO or mssql_connect.
On the PHP documentation i find:
Mssql_connect - The familiar expression:
<?php
// Create a link to MSSQL
$link = mssql_connect('KALLESPC\SQLEXPRESS', 'sa', 'phpfi');
// Select the database 'php'
mssql_select_db('php', $link);
?>
PDO - Which i haven't tried before - which needs a driver !(?) :
<?php
/* Connect to an ODBC database using driver invocation */
$dsn = 'mysql:dbname=testdb;host=127.0.0.1';
$user = 'dbuser';
$password = 'dbpass';
try {
$dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
?>
So what to choose and why ?
Although I have not tried it yet. So I can't say for sure if it works or not.
PHP Manual says use pdo::dblib http://php.net/manual/en/ref.pdo-dblib.php
Microsoft does have it's own set of drivers but you have to be on a windows machine to use them. http://www.microsoft.com/en-us/download/details.aspx?id=20098
MSSQL connection with PDO:
$db_handle = new PDO("sqlsrv:server=$server; Database=$database", $user, $pass);
MySQL connection with PDO:
$db_handle = new PDO("mysql:host=$server;dbname=$database", $user, $pass);
I don't see what your confusion is?

Categories