I have a LAMP stack, and I thought it might be nice to use the MySQL configuration file to allow PDO to connect to the MySQL database without the need to place DB credentials within our code structure
So I created a group in /etc/my.cnf.d/gggg.cnf
[clientGggg]
user=uuuu
password=pppp
database=dddd
and can now connect to the DB through the command line with mysql --defaults-group-suffix=gggg
I then tried to create a test DB connection in PHP with
$db = new PDO("mysql", null, null, [PDO::MYSQL_ATTR_READ_DEFAULT_GROUP => 'gggg']);
but I received the error
Undefined constant PDO::MYSQL_ATTR_READ_DEFAULT_GROUP
Checking the PDO docs for this attribute reveals the following:
PDO::MYSQL_ATTR_READ_DEFAULT_GROUP (int)
Read options from the named group from my.cnf or the file specified with MYSQL_READ_DEFAULT_FILE. This option is not available if mysqlnd is used, because mysqlnd does not read the mysql configuration files.
and checking my phpinfo(); reveals I am indeed using mysqlnd
Why is this limitation in place? Is there a workaround? Or am I doomed to write/load my credentials into PHP?
Trying to get it to connect in this environment:
Windows 7 Pro - 64bit
MS Access 2010 - 32bit (Tried both ACCDB and MDB versions)
ADODB abstraction package, Version 5.20.9
This is the test code:
include("C:\php\adodb5\adodb.inc.php"); // includes the adodb library
$db = NewADOConnection("access"); // A new connection
$db->Connect("", "", "", "D:\...\PhpPlay.accdb");
I've tried all kinds of variations for host, user and password params in the $db-> connect line, but no success. (The first two lines execute without error.) Here's the error message for line 3:
PHP Warning: odbc_connect(): SQL error: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified, SQL state IM002 in SQLConnect in C:\php\adodb5\drivers\adodb-odbc.inc.php on line 66
So I'm grasping at straws now. I'm concerned about the mixed 32/64 bit settings, but don't want to do something drastic unless someone can confirm that this is a problem.
Thank you for any ideas you might be able to provide!
Third party modules are not needed. Simply use PHP's PDO class and the already installed MS Access ODBC driver:
DSN Version
$database="D:\...\PhpPlay.accdb";
$db = new PDO("odbc:DSN=MS Access Database;DBq=$database;");
Driver Version
$database="D:\...\PhpPlay.accdb";
$db = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBq=$database;");
Good day everyone, i have an application that uses .dbf files to store data and i would like to run some queries on those files. the problem is that i don't know how .. i've spoken with the people from support about it and they told me that i can an ODBC driver or the dedicated one for visual foxpro Ole DB. I found something called dabse in php manual yet on php 5.3 it doesn't support it or i didn't install it right because it didn't work, from what i've read on google seems that it supports until php 5.2 or something like that.
Can you help me figure it how to do it ? i've googled around but couldn't find anything that might help me out.
Edit
$excelFile = realpath('C:\\db\\article.dbf');
$excelDir = dirname($excelFile);
$dsn = "DRIVER={Microsoft dBase Driver (*.dbf)};SourceType=DBF;SourceDB=C:\\db\\article.dbf;DefaultDir=$excelDir;Exclusive=NO;collate=Machine;NULL=NO;DELETED=NO;BACKGROUNDFETCH=NO;";
$conn=odbc_connect($dsn,"","");
$sql = "SELECT * FROM articole.dbf";
$result = odbc_exec($conn, $sql);
The Error:
Warning: odbc_exec() [function.odbc-exec]: SQL error: [Microsoft][ODBC dBase Driver] External table is not in the expected format., SQL state S1000 in SQLExecDirect in E:\public_html\odbc\index.php on line 7
Do you have SQL Server? You could create a linked server to the DBF files. Check out this link:
Linked Server
See also these links for querying DBase or Foxpro tables with ODBC or OLE DB:
Dbase ODBC
Foxpro OleDB
I am developing an application that needs to get data from an outside MSSQL database. I spent a lot of time trying to get various methods of connection to MSSQL with PHP, but there were several routes which were depreciated.
On my production environment running Debian, I was able to make a connection with PDO_DLIB and FreeTDS with something like this:
$this->db = new \PDO('dblib:host='.$thedb_host_prod.';dbname='.$thedb_database_name_prod, $thedb_database_user, $thedb_database_pass);
On Windows, MSSQL is depreciated. I believe I'm using the Microsoft SQL Server Driver and was only able to get it to work with ODBC, which looks something like this:
$dsn = "Driver={SQL Server};Server=".$thedb_host_dev.";Database=".$thedb_database_name_dev;
$this->odbc = odbc_connect($dsn, $thedb_database_user, $thedb_database_pass);
Then, the problem becomes, in each method I need to do something differently for ODBC than I do for DLIB.
public function exampleMethod(){
// logic and create the query in $query
if($this->dev == false){
// PRODUCTION
try {
$stmt = $this->db->prepare($query);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_OBJ);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
} else {
// DEVELOPMENT
$query = $query;
$stmt = odbc_exec($this->odbc, ($query));
$result = array();
while($currentRow = odbc_fetch_object( $stmt )){
$jobNumber = $currentRow->Code; // Set object key to jobNumber
array_push($result, $currentRow);
}
}
}
This actually works, but the problem is, with how the query for ODBC needs to be prepared vs how the DBLIB query should be prepared, means that if I don't want to write the query twice in each method, I have to create it before each action. This is really bad because it means I'm not putting my variables into the query with PDO's bindValue.
So, has anyone been able to get PDO work with PHP 5.4 and MSSQL in a Windows environment? Does anyone see a way of securing the query in a way that doesn't make me duplicate the query in each method, once for ODBC and once for DBLIB?
My plan currently is to develop the application out and then remove all of the ODBC stuff which will allow me to put the query in the $stmt properly, avoiding this problem. But until then, it's making development a huge pain.
Oooook, so I finally got it working !
Here are the libraries / drivers for Sql-server connection via C/C++/java/PHP/etc.
Precisely, here are the Windows drivers for PHP.
Check out this page to know which version you need to get
They all exist in have 32 and x64, as well as "Thread safe" (ts) and "non-thread-safe" (nts) versions.
From what I read, nts versions are to be used if you work with IIS.
Usage :
Download and extract the package you need.
in my case, package 3.2, for php 5.6
Take necessary drivers
in my case, php_sqlsrv_56_ts.dll and php_pdo_sqlsrv_56_ts.dll
Put them in your php extensions directory
in my case, [...]\php5630vc11x86x170623162800\ext
If you wonder where this dir is inside your PHP, it should be quite easy to find, since you have plenty of other dlls here.
Likely, php_pdo_mysql.dll, php_pdo_pgsql.dll, etc.
Modify your php.ini
Be careful with Wamp, it seems to have one in the php directory AND another one in the apache dir.
Add lines to load the two extensions.
You can add them at the end of your file, or with the other extensions loading.
In my case, here's what I added :
;SQL-srever extensions
extension=php_sqlsrv_56_ts.dll
extension=php_pdo_sqlsrv_56_ts.dll
This part was for the PHP/PDO side.
For the driver to actualy work, you also need your (Windows) machine to have the ODBC drivers installed.
Feel free to try your connection already, but if it whines that you need the ODBC driver, go get it here :
Microsoft® ODBC Driver 11 for SQL Server®
And last, but not least, be sure to use the right format for your PDO connection.
$conn = new PDO ("sqlsrv:Server=$srv_host;database=$srv_dbname";
For comparison, here's what I wsa using on my linux server :
$conn = new PDO ("dblib:host=$srv_host:$srv_port;dbname=$srv_dbname", "$srv_username", "$srv_password");
I thought this syntax was common to all PDO drivers, but it appears I was wrong.
Here is the PHP PDO driver documentation.
I just actually had to do some work in php connecting to a MSSQL server. I did have to downgrade to php 5.4 due to the fact that the php_pdo_sqlsrv.dll is not updated for 5.5. For the dll files check here. But now down to the code I used to connect once you have the .dll files in the right place.
try {
$db = new PDO("sqlsrv:Server={$host};Database={$database}", $userName, $password);
}catch(PDOException $e){
die("failed to connect");
}
Just a standard PDO connection. Just in order to get it to work you must make sure that the .dll files are in the php directory.
I hope that answers at least part of the question.
I have worked with PHP 5.4 and SQL Server with PDO on Windows.
I strongly recommend using Microsoft's Web Platform Installer to set everything up. You can use it to install PHP, a local version of SQL Server Express to develop on, the official PHP driver for SQL Server, and IIS, all set up to work together.
One note of caution: The last release of the SQL Server PDO driver was in April, 2012. I reported a bug against it last year and was told that it's in "limited support", which apparently translates to "you're on your own". In any case, it worked reasonably well.
ADODB Error: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
I downloaded the odbc driver for PHP. I pasted that driver in the PHP ext directory, then I get the error. Can anybody help me?
A few things that I would try,
Make sure that the extension is included in your php.ini
Make sure that you've extracted the db driver to the right place c:\windows\drivers in some cases!
Have you rebooted? As you'll need to reboot your server to load the driver, I assume you have as you get the error :)
As the comment above states, which driver you are using, it's version, and which database you're connecting to would also be handy to know :)
I assume that you're using PHP5 to connect to MSSQL2005. In which case the driver is really dodgy, or was when I last worked with it. It would maintain a connection about 1/3 tries. What web server are you using? As I recall when we were using IIS to serve the PHP+MSSql we had some configuration to do in the IIS setup.
try:
# config file
$oODBC = new stdClass();
$oODBC->driver = "odbc_mssql";
$oODBC->dns = "Driver={SQL Server};Server=127.0.0.1;Database=yourdatabhase;";
$oODBC->user = "username";
$oODBC->pass = "password";
# connect
$mDB = $oODBC;
$db =& ADONewConnection($mDB->driver); // ex: odbc_mssql
$db->PConnect($mDB->dns, $mDB->user, $mDB->pass);