Connect MS Access DB In Linux Using PHP - php

i have site hosted on bluehost.com (Linux Server), i have use case that i have to Export some Specific Data Export to MS Access File,
I have following code to Connect with MS Access File:
$dbh = null;
try{
$dbh = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb)};Dbq=$file_path", $user_name, $password, $db_info);
}catch(PDOException $e){
echo $e->getMessage();
}
return $dbh;
But when i run the above code on Hosting server than i got error
SQLSTATE[IM002] SQLDriverConnect: 0 [unixODBC][Driver Manager]Data source name not found, and no default driver specified
Later after some searching i changed the code of DB connection to
try{
$conn = odbc_connect("Driver={Microsoft Access Driver (*.mdb)};Dbq=$file_path", ACCESS_DB_USERNAME, ACCESS_DB_PASSWORD);
}catch(Exception $ex){
echo $ex->getMessage();
}
in above code i did not get any exception but $conn is NULL, both code snippet working fine on my local Windows machine,
Can you people help me in fixing issue?? i want to make connection and want to run INSERT INTO statement on MS Access DB.

The docs for odbc_connect say it returns one of two things.
An ODBC connection.
FALSE on error.
Your code should look for FALSE, not for an exception.
The docs also have examples of several different kinds of connections. But I think the chances that Bluehost installs Microsoft Access drivers on all their Linux servers are less than zero.

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

SQL Server PDO could not find driver

I'm using Microsoft Azure SQL for my database and I'm trying to connect it to my project on my localhost using XAMPP. When I try to connect to the database using the connect string they provided:
try {
$conn = new PDO("sqlsrv:server = tcp:app.database.windows.net,1433; Database = mydatabase", "{myusername}", "{your_password_here}");
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e) {
print("Error connecting to SQL Server.");
die(print_r($e));
}
I keep getting this error:
Error connecting to SQL Server.PDOException Object ( [message:protected] => could not find driver
The error is longer but I think it's related to the driver mentioned at the end. I'm using a mac so any driver I need to install would need to be compatible with it. Thank you for the help in advance.
Azure SQL Database is built on the Microsoft SQL Server engine. So you might consider using pdo-dblib to connect to SQL Server using the PDO on a Mac. You can check out a Stack Overflow question about it here or refer to this setup documentation.
After the driver having been installed, Azure SQL won’t actually accept connection from your local yet. And you might get the following error:
Client with IP address '167.xxx.xxx.xxx' is not allowed to access the
server.
That’s because there is a firewall in the way by default. To enable access, go to the Azure portal, click on All Resources, select your SQL service, click on Firewall in the SETTING menu.
Your client address is conveniently included in the list, so you can just click on Add client IP followed by Save.
Well, when you run your code now, it should connect.

Can't connect my Microsoft access database file using php (Xampp)

I made a php webpage with login and password it used run on my previous system (xampp was ran database in that system) but after installed xampp in my system I started Apcahce and Mysql (running succesfully). The login page ran all fine but once i tried to login it s giving me this error
**could not find driver
Notice: Undefined variable: db in C:\xampp\htdocs\FINALschool\GLOBAL1.php on line 12
Fatal error: Call to a member function query() on null in C:\xampp\htdocs\FINALschool\GLOBAL1.php on line**
The on my link to db folder is
<?php
$dbName = $_SERVER["DOCUMENT_ROOT"] . "\FINALSCHOOL\REG.MDB";
if (!file_exists($dbName)) {
die("Could not find database file.");
}
try{
$db = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb)};Dbq=$dbName;Uid=Admin");
}
catch(PDOException $e){
echo $e->getMessage();
}
?>
I have also checked my user name and password for the login with my database and it is all the same so please help
Also my xampp is connected to it's default port
You can't just point to the .mdb file as a data source, you have to set up a MSAccess ODBC source:
Start at Start > Settings > Control Panel > Administrative Tools > Data Sources (ODBC)
Then for your $dbName you use the DSN you chose as the name of source (e.g. "IguanaTestData" below).
Reference

Connecting to a MS Access database

Someone asked me to do some research to achieve the following result, maybe you guys can help me out an suggest me some tips.
We have a local server and there are different 'jobs' on it ( in a MS Access ) Database.
Now we want to create a platform where other users can check their "job status" with different parameters ( field names etc ) from the MS Access database.
I am looking for the best practice, how to connect to this database from everywhere in the world. I already did some research and found the following links:
http://phpmaster.com/using-an-access-database-with-php/
http://www.php.net/manual/en/function.odbc-connect.php
How can I connect between a web-application and that MS Access database
Can I update it realtime
What kind of protocol does the servers has to support to intereact with a website?
Currently the server can be reached from outside via a VPN connection.
A piece of code I've already tried :
try{
$dbh = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=Custos_TAB.accdb;Uid=Admin");
}catch(PDOException $e){
echo $e->getMessage();
}
I received the following error : "could not find driver".
Try this, assuming you have the PDO odbc driver installed and enabled on the web server.
$user='Admin';
$password='';
$mdbFilename="Custos_TAB.accdb";
$connection = odbc_connect("Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=$mdbFilename", $user, $password);
$sql="SELECT * FROM [tblHere]";
$rs=odbc_exec($connection,$sql);
//do stuff here
odbc_close($connection);
or
$user='Admin';
$password='';
/*if you have a path here such as c:\db\Custos_TAB.accdb, make sure to use double
backslashes, (i.e "c:\\db\\Custos_TAB.accdb")*/
$mdbFilename="Custos_TAB.accdb";
$conn = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=$mdbFilename;Uid=$user='Admin';Pwd=$password;");
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
Stop running services first (if there's any)
Open the php.ini file and uncomment the extension for php_pdo_odbc
Start services again (if needed)
Make sure you correctly locate your database in Dbq=Custos_TAB.accdb
//Just an example in my case
Dbq=C:\Users\Server\Documents\Db1.accdb

Why can I connect to a local SQL Server Express but not to a remote SQL Server from PHP?

This is the standard code when using windows authentication:
<?php
try {
$conn = new PDO("sqlsrv:Server=geoffrey-pc\SQLEXPRESS;Database=books",
NULL, NULL);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo $e;
die("Error connecting to SQL Server");
}
echo "Connected to SQL Server\n";
?>
The above works for connecting to the local server (SQL Server 2008 Express Edition), but not for connecting to a server on the network (SQL Server Standard Edition). The error message is:
exception 'PDOException' with message 'SQLSTATE[28000]:
[Microsoft][SQL Server Native Client 10.0][SQL Server]Login failed for
user 'myDomain\GEOFFREY-PC$'.' in C:\wamp\www\PhpProject1\index.php:10
Stack trace: #0 C:\wamp\www\PhpProject1\index.php(10):
PDO->__construct('sqlsrv:Server=n...', NULL, NULL) #1 {main}
The connection string for the network server is the same as for the local server, except that the server name is similar to abc0120 and does not end with \SQLEXPRESS or anything else, and the database name is different. The database name I use with the network server does exist.
I am using Apache 2.2.11. The SQLServer 2005 MSDN page for How to: Connect Using Windows Authentication reads:
The credentials under which the Web server's process (or thread) is running must map to a valid SQL Server login in order to establish a connection.
Maybe that is the problem.
I can connect to the network server using SQL Server Management Studio also using windows authentication.
From the PHP manual, you have this example :
<?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();
}
In your example, you omit the $user and $password arguments. I would try and specify a username and password instead of passing null; the user running your PHP script might not be the same as the logged user... omitting to provide a username and password, PHP tries to provide the user's credentials who's running the script (with perhaps even no password at all).

Categories