I've installed the php5-odbc library.
Trying to connect to a .mdb file but keep getting error Data source name not found, and no default driver specified.
Code:
$dbName = "../../../var/Import/PartsPlaceDB.mdb";
if (!file_exists($dbName)) {
die("Could not find database file.");
}
$db = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=$dbName; Uid=; Pwd=;");
Outputs:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[IM002] SQLDriverConnect: 0 [unixODBC][Driver Manager]Data source name not found, and no default driver specified' in [...]
PDO Drivers:
foreach(PDO::getAvailableDrivers() as $driver)
{
echo $driver.'<br />';
}
Output:
mysql
odbc
sqlite
The problem is recurrent with 64-bit version. It looks like your code only work on Windows 32-bit.
To solve the problem, you might install this tool: Microsoft Access Database Engine 2010 Redistributable. I had the same problem for months but this tool solved mine.
I guess you're doing the location wrong?
Instead of this
$dbName = "../../../var/Import/PartsPlaceDB.mdb";
Why not do it like this
$dbName = "..\..\..\var\Import\PartsPlaceDB.mdb";
Related
I am trying to connect the Microsoft access database using PDO with odbc. I have mounted the files on network drive and I am trying to access them, but I am getting the following error:
PDOException' with message 'SQLSTATE[01000] SQLDriverConnect: 0 [unixODBC][Driver Manager]Can't open lib 'Microsoft Access Driver (*.mdb)'': file not found'.
Here is my code:
$dbName = "/info/new.mdb";
if (!file_exists($dbName)) {
die("Could not find database file.");
}
$database = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb)};Dbq= $dbName;Uid=admin");
First, make sure the odbc extension is activated in the php.ini file. Just remove the ; to enable it.
;extension=php_pdo_odbc.dll
Then for connection, please use the exact file location with proper convention (it should be using backslash). The example is below.
try {
$file_location = "C:\Users\PC1\Desktop\your_database.mdb";
$dbh = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb)};Dbq=$file_location;Uid=Admin");
//Do your program stuffs here
$dbh = null;
} catch (PDOException $e){
echo $e->getMessage();
}
I just installed the Zend Server on my system and I'm trying to test my database connections. I prefer using PDO to connect my databases.
MySQL connection works very well, but when I'm trying to connect MS SQL server I got an error.
Connection to MSSQL Server failed: SQLSTATE[IM002] SQLDriverConnect: 0 [unixODBC][Driver Manager]Data source name not found, and no default driver specified
This is the code that I'm trying to run
try {
$pdo = new PDO('odbc:host=ip-addr; dbname=my-database', 'user', 'password');
echo "Successfully connected to MSSQL Server";
} catch (PDOException $e) {
echo "Connection to MSSQL Server failed: " . $e->getMessage();
}
I have successfully enabled odbc and pdo_odbc in the configuration panel. I'm running this on macOS Sierra 10.12.
Thanks in advance.
I found this tutorial, and it brought me to succeed. It's written for Linux, but I just installed Homebrew and followed the tutorial a bit adapting.
I am receiving an error as below:
PHP Fatal error: Uncaught exception 'PDOException' with message
'SQLSTATE[08001]: [Microsoft][ODBC Driver 11 for SQL Server]SQL Server
Network Interfaces: Connection string is not valid [87]. '
My codes are as follow:
$this->link = new PDO(
"sqlsrv:server=$this->serverName:$this->port;Database=$this->db",
"$this->uid",
"$this->pwd"
);
I wish someone can enlighten me on what is causing this error. Before that I was using dblib instead of sqlsrv.
"dblib:host=$this->serverName:$this->port;dbname=$this->db"
I am using XAMMP with Apache 2.4.12 and PHP 5.5.24 (they are all x86). I have php_pdo_sqlsrv_55_ts.dll and php_sqlsrv_55_ts.dll. I am also using Microsoft ODBC Driver 11 for SQL Server-x64.
Change it to:
$this->link = new PDO(
"sqlsrv:Server={$this->serverName},{$this->port};Database={$this->db};",
$this->uid,
$this->pwd
);
The default SQL Server port is 1433. Note the curly brackets, they allow for class variables.
I am trying to access a MDB file using PHP on a CentOS machine, preferably using PDO. I have installed the ODBC and pdo_odbc modules in PHP and restarted the the web server (Apache). I also installed mdbtools and unixODBC.
print_r(PDO::getAvailableDrivers());
Shows:
Array
(
[0] => mysql
[1] => odbc
[2] => pgsql
[3] => sqlite
)
I try to access the DBA file (which is saved locally) with this code:
try
{
$driver = "/usr/lib64/libmdbodbc.so.0";
$dbName = "/PATH/FILE.MDB";
if (!file_exists($dbName)) {
die("Could not find database file.");
}
$db = new PDO("odbc:DRIVER=$driver; DBQ=$dbName; Uid=; Pwd=;");
$sql = "SHOW TABLES";
$result = $db->query($sql);
$row = $result->fetch();
var_dump($row);
}
catch(Exception $e)
{
echo "\n\nEXCEPTION: $e\n\n";
}
And this outputs:
EXCEPTION: exception 'PDOException' with message 'SQLSTATE[IM002] SQLDriverConnect: 0 [unixODBC][Driver Manager]Data source name not found, and no default driver specified' in /PATH/odbctest.php:14
Stack trace:
#0 /PATH/odbctest.php(14): PDO->__construct('odbc:DRIVER=/us...')
#1 {main}
What do I need to change? What should I use as a driver name? I have been looking all over the place and all I find is a lot of really old stuff from the 2004 era, but a lot of the links have disappeared over the years. This is the most relevant StackOverflow question, but does not include any code: PDO Microsoft Access. I really only need to read from the file.
EDIT:
With some tinkering it seems that I now at least have PDO happy with the driver by correcting the odbcinst.ini file which now contains the following:
[MDBTools]
Description = MDBTools Driver
Driver64 = /usr/lib64/libmdbodbc.so.0
Setup = /usr/lib64/libmdbodbc.so.0
FileUsage = 1
UsageCount = 1
Unfortunately, I am still getting an error:
exception 'PDOException' with message 'SQLSTATE[08001] SQLDriverConnect: 1 [unixODBC]Could not find DSN in connect string' in /PATH/odbctest.php:15
Stack trace:
#0 /PATH/odbctest.php(15): PDO->__construct('odbc:Driver=MDB...')
#1 {main}
I am making the PDO call as follows:
$driver = "MDBTools";
$dbName = "/PATH/DATABASE.MDB";
$db = new PDO("odbc:Driver=$driver; DBQ=$dbName; Uid=; Pwd=;");
I tried adding the data source to the odbc.ini file even though I can't do this in the production version and am still getting the same error. I did find this which is interesting: DSN-less connection with PHP ODBC using MDBTools Driver. Any suggestions?
$driver = "MDBTools";
$dbName = "/PATH/DATABASE.MDB";
$db = new PDO("odbc:Driver=$driver;DBQ=$dbName", "", "");
I just updated my Windows Xampp install to get the latest PHP version and after the reinstall my web app is giving me the following error when trying to connect to an Oracle data source via the Microsoft ODBC driver for Oracle.
Warning: odbc_connect(): SQL error: [Microsoft][ODBC driver for
Oracle][Oracle]ORA-12154: TNS:could not resolve service name, SQL
state 08001 in SQLConnect in C:\xampp\htdocs\webapp\connections\db.php
on line 34
The code I'm using for this is;
$dsn = 'dsnname';
$user = 'username';
$pass = 'password';
$conn = odbc_connect($dsn, $user, $pass, SQL_CUR_USE_ODBC);
This was working fine before I did the Xampp update and now suddenly it's not working. I think I had this problem once before but I'm convinced it was solved by adding the SQL_CUR_USE_ODBC cursor.
Anyone got any ideas on what could be wrong?