PHP Cannot connect to PDO ODBC Driver - php

My php cannot find my odbc driver. I've downloaded and re-installed multiple times. Can anyone help me with this error:
QLSTATE[IM002] SQLDriverConnect: 0 [Microsoft][ODBC Driver Manager] Data
source name not found and no default driver specified"
Here is my php code:
$dbName = "C:\Users\David\Documents\SCHOOLNEW\Assignment5-PROG1800\database\as4.mdb";
if (!file_exists($dbName))
{
die("Could not find database file.");
}
try
{
// Connect
$dbh = new PDO("odbc:Driver={Microsoft Access Driver(*.mdb, *.accdb)};Dbq=C:\Users\David\Documents\SCHOOLNEW\Assignment5-PROG1800\database\as4.mdb;Uid=Admin");
// INSERT data
$count = $dbh->exec("INSERT INTO part(vendorNo,description,onHand,onOrder,cost,listPrice) VALUES ('$vendorNo', '$desc', '$onHand', '$onOrder', '$cost', '$listPrice')");
// echo the number of affected rows
echo $count;
// close the database connection
$dbh = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
I'm running php with apache on xampp. This all on a local machine. My system is 64 bits. I'm not sure if it has something to do with the system and drive types or my syntax or certain drivers I need to install. I just want to insert data from my form into the database on my computer.

Driver={Microsoft Access Driver(*.mdb, *.accdb)}
is not a valid ODBC driver name because it is missing a space. The correct name for the newer "ACE" ODBC driver is
Driver={Microsoft Access Driver (*.mdb, *.accdb)}
However, in this case PHP is running in the 32-bit environment and trying to open an .mdb database so the older "Jet" ODBC driver ...
Driver={Microsoft Access Driver (*.mdb)}
... will work, too.

Can you place the path after escaping the slashes and then try:-
$dbName = "C:\\Users\\David\\Documents\\SCHOOLNEW\\Assignment5-PROG1800\\database\\as4.mdb";
Escape the slashes in all the paths you have provided in the code and then try.

Related

How to set up an ODBC for MS Access 2016?

I am fairly new to databases and also my English is very bad so don't mind my silly mistakes. I am using:
MS Access 2016 to create a local database named as tc.accdb
XAMPP to provide local host connectivity
this php code to check whether local connectivity is achieved or not:
<?php
$con = odbc_connect( "tc" , "" , "" );
if($con)
{
echo "Connected";
}
else
{
echo "Failed" ;
}
?>
But I just cannot set up Microsoft Access driver for my database. Under administrator tools >.....> System DSN, there exist Microsoft Access driver for (*.mdb) .But since I'm using Access 2016, my database has (.accdb) extension.
I've also tried saving my database as (database.mdb) to avoid extension confilicts but I've used some Office16-specific features so it can't be saved as a database with (.mdb) extension.
Note
Using (.mdb) connectivity with my (.accdb) database just didn't do the job as when I try to run the check code it gives me following error.
try this
$connStr = 'odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};' .
'Dbq=C:\\Users\\Igor\\Desktop\\example.accdb;';
$dbh = new PDO($connStr);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
good luck

Connect IBM database using PDO

I tried connect IBM database through PDO using below code. But, it is not working
try {
$db = new PDO("odbc:DRIVER={IBM DB2 ODBC DRIVER};DATABASE=BLUDB;HOSTNAME=hostname;PORT=50000;PROTOCOL=TCPIP;", "username", "password");
echo "<pre>";
print_r($db);
exit;
} catch (PDOException $e) {
echo $e->getMessage();
}
I got below error for the same
SQLSTATE[IM002] SQLDriverConnect: 0 [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
I have also added below code in php.ini file
extension=php_pdo.dll
extension=php_pdo_ibm.dll
extension=php_ibm_db2.dll
Could anyone suggest me, how I can connect with IBM database?
The DSN prefix for DB2 databases is ibm:, not odbc:. Try changing that.
Here is the example connection string given in the documentation:
$db = new PDO("ibm:DRIVER={IBM DB2 ODBC DRIVER};DATABASE=testdb;" .
"HOSTNAME=11.22.33.444;PORT=56789;PROTOCOL=TCPIP;", "testuser", "tespass");
there is different driver name for some computer, this is the list of driver you can try:
DRIVER={iSeries Access ODBC Driver};
DRIVER={IBM i Access ODBC Driver};
and try to use System instead of HOSTNAME
DRIVER={iSeries Access ODBC Driver};DATABASE=BLUDB;System=hostname;PORT=50000;PROTOCOL=TCPIP;
oh, and I'm using DRIVER={IBM i Access ODBC Driver};

Unable to connect to access database using php

I am unable to connect to access database through php.
I have searched the internet for the issue. It is my 3rd day, I am trying this but could not find the solution.
I have installed all the required things: ODBC drivers and MS Access Database engine.
My php code to connect to database named newDB.mdb is:
<?php
$dbName = $_SERVER["DOCUMENT_ROOT"] . "access_db/newDB.mdb";
echo $dbName."<br />";
if (!file_exists($dbName)) {
die("Could not find database file.<br />".$dbName);
}
try {
$db = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb,*.accdb)};Dbq=$dbName");
} catch(PDOException $e) {
echo "Error: ".$e->getMessage()."<br />";
}
?>
The error I receive:
C:/wamp/www/access_db/newDB.mdb
Error: SQLSTATE[IM002] SQLDriverConnect: 0 [Microsoft][ODBC Driver Manager]
>Data source name not found and no default driver specified
I am working on a local machine.
OS: Windows8 Pro, and WampServer 2.5, Microsoft Access 2013 (I am exporting the file as .mdb, it also doesn't work for .accdb extension)
I don't know what I am lacking, or what I need to do.
Your driver name is missing a space. It should be
DRIVER={Microsoft Access Driver (*.mdb, *.accdb)}
^
As long as just needing to support .mdb-files, you can try this:
$db = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb)};Dbq=$dbName");
This worked fine for me.
It seems, on default the "Microsoft Access Driver (*.mdb, *.accdb)" is not installed. Open C:\Windows\SysWOW64\odbcad32.exe in the register card Drivers.
In my case - I think that's the standart office/access installation behavior - ther was just the Microsoft Access Driver (*.mdb).
If you need to support .accdb-files, you'll need to install the "Microsoft Access Database Engine 2010 Redistributable": https://www.microsoft.com/en-us/download/details.aspx?displaylang=en&id=13255
After installing, you can use
$db = new PDO("odbc:DRIVER={Microsoft Access Driver
(*.mdb,*.accdb)};Dbq=$dbName");
Edit:
I found a newer version for my installation of Office 2016 (https://www.microsoft.com/en-us/download/details.aspx?id=54920) but was unable to install.
I got errors reporting for using 64-bit office components, but 32-bit office is installed. Anybody an idea to solve the problem?

Having trouble with ODBC Driver for Access database

I have created an intranet php site using WampServer on my home test environment where everything works fine. I then tried to install it on the production server, again with WampServer installed, but when I try to connect to the database, I get an error:
ERROR: SQL STATE[IM002] SQLDriverConnect:0 [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified.
I have the php_pdo_odbc extension enabled.
Here is the code I'm having trouble with:
$dbName2013 = $_SERVER["DOCUMENT_ROOT"] . "/Ridley/RLCompRepair.accdb";
try {
$db = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};;Dbq=$dbName2013");
}
catch (Exception $e) {
echo "ERROR: ".$e->getMessage();
}
Any and all help would be greatly appreciated.
Chris
You are trying to connect with DNS-less database.
create a Windows ODBC DNS
In Windows go to "Administrative Tools" and Select "Data Sources"
Go to File DSN select Add
select Microsoft Access Driver (*.mdb)
Select Next, type in a Data source Name
select "Next" then Finish
You should be in ODBC Microsoft Access Setup
select "Select" navigate to your .mdb and select it.
select "OK"
In your PHP try this:
$connect = odbc_connect("[data source name given]", "user", "password");
$connect = odbc_connect("myaccess", "", "");
if($connect === false){echo "did not work<br>";exit}
echo "success";

What will be connection string to Access database file with PHP

I installed WAMP, I have access database file in project folder, but don't have installed Access on my computer.
Can I read and update Access file with PHP even I don't have installed Access?
And what will be connection string to Access database file?
I really need help with this.
All you need is the PHP api for ODBC.
Here is the example from the docs itself:
<?php
// Microsoft SQL Server using the SQL Native Client 10.0 ODBC Driver - allows connection to SQL 7, 2000, 2005 and 2008
$connection = odbc_connect("Driver={SQL Server Native Client 10.0};Server=$server;Database=$database;", $user, $password);
// Microsoft Access
$connection = odbc_connect("Driver={Microsoft Access Driver (*.mdb)};Dbq=$mdbFilename", $user, $password);
// Microsoft Excel
$excelFile = realpath('C:/ExcelData.xls');
$excelDir = dirname($excelFile);
$connection = odbc_connect("Driver={Microsoft Excel Driver (*.xls)};DriverId=790;Dbq=$excelFile;DefaultDir=$excelDir" , '', '');
?>
// Microsoft Access
Open the Administrative Tools icon in your Control Panel.
Double-click on the Data Sources (ODBC) icon inside.
Choose the System DSN tab.
Click on Add in the System DSN tab.
Select the Microsoft Access Driver.
Click Finish.
In the next screen, click Select to locate the database.
Give the database a Data Source Name (DSN).
Click OK.
$dsn='database.accdb';
$username='';
$password='';
$connect=odbc_connect($dsn, $username, $password);
I'v found this link with a tutorial on how to do it. Be careful that things work differently in windows and UNIX environment, but since you are using a WAMP you should have no problems
<?php
$db = $_SERVER["DOCUMENT_ROOT"] ."/AccessDatabase/reg.accdb"; //AccessDatabase is folder in htdocs where the database is store
if (!file_exists($db))
{
die("No database file.");
}
$dbNew = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb, *.accdb)}; DBQ=$db; Uid=; Pwd=;");
$sql = "select * from reg"; //reg is table name
$rs = $dbNew->query($sql);
while($result = $rs->fetch())
{
echo $result[0].": ".$result[1].": ".$result[2]."<br />";
}
?>
If u got error like pdo ODBC Drivers not installed
just go to php.ini and find extension = pdo_ODBC Driver and remove the comment(;)
after that restart the apache

Categories