LAMP (PHP) accessing Access Database with ODBC with MDBTools Driver - php

Can anyone tell me if there is limited SQL support using unixODBC drivers on Ubuntu with PHP? I've setup a basic lamp server on Ubuntu 11.10, and I'm trying to query an Access database. I've installed php5-odbc and MDB Tools. Here is some sample code:
<?php
$conn = odbc_connect('logindb','','');
if (!$conn) {
echo "failed";
}
$sql = "SELECT * FROM class";
//$sql = "SELECT class.desc, event_classes.event_class_id FROM class inner join
//event_classes on class.class_id = event_classes.class_id";
$rs = odbc_exec($conn, $sql);
while ($d = odbc_fetch_array($rs)) {
var_dump($d);
}
?>
The first query, the simple select, works just fine. However, when trying to perform a join (second commented query), I receive 0 records. I was able to run the SQL successfully in the SQL editor within Access, so I know my SQL is correct and the join is working there. Has anyone had any experience with this? I know Linux wasn't made to be compatible with proprietary Microsoft products, but unfortunately I have to make reading an Access database on Ubuntu work. Any help is greatly appreciated.

mdbtools on sourceforge has not been updated since 2004. I see someone has put it on github and seems to have made a few small changes. The SQL support in it was never that good, it was read only and I know from experience trying it under Perl with DBD::ODBC a lot of the tests failed. I think you are going to have a hard time with it.
There are other more reliable ways to access a MS Access database from Linux but they are all to my knowledge commercial. To access a MS Access database from Linux I know of the following:
Easysoft have an ODBC-ODBC Bridge (which can be used to access a MS Access DB on a windows machine using the MS Access ODBC Driver) but it requires installing a service on the Windows machine. Easysoft also have a MS Access ODBC driver which can be used for direct access to the mdb/accdb file so long as it is visible from Linux.
There are other commercial ODBC bridges from Openlink.

If anyone happens to be looking for a hacky solution, I ended up using mdb-tools to convert the access database to csv files (1 per table), and then iterating through the csv performing a "manual" join. Not the best solution, but it ended up working for fairly small tables. Hope it helps!

Related

Running PHP on iOS?

I have an app that uses a few php scripts to access a mysql database.
Can anyone advise on how to implement an offline version?
I understand it is easy to create an SQLLite database in the app and populate it when the web server is available? true?
When I want to query it, is it possible to do that using the pre-existing php?
thanks in advance
HMJ
It's important that you make the distinction between the components you are speaking of - PHP is a server side language that is meant to carry out the DB queries. SQLite is the iOS framework that allows us client-side DB management on the iOS operating system. On the iPhone you can carry out the same queries that are generated using your PHP but they are to be built using objective-c. There are some useful wrappers that do this, including FMDB.
Kodiak PHP runs on an iPad. It has PDO installed. You can create a local sqlite database. I tested it with this code:
<?php
error_reporting(E_ALL);
$db = new PDO('sqlite:test.sql');
$n = $db->exec('DROP TABLE IF EXISTS USER');
$n = $db->exec('CREATE TABLE user (login TEXT)');
$n = $db->exec('INSERT INTO user VALUES("joe")');
$r = $db->query('SELECT * FROM user');
foreach ($r as $row) {
echo $row['login']."\n";
}
?>
In regard to PH7... it just implements the PHP language and a some functions, it does not support all PHP functions nor extensions. For instance, PCRE is often used for regular expression processing. PH7 does support it. PH7 is a rewrite, intended for embedded applications, basically a more capable scripting engine such as Lua, but not as big a footprint as Javascript.
Kodiak PHP is an actual PHP port. Use phpinfo(); to see what is has compiled into it, it's quite a bit.
To run your SQL scripts in an iOS app, you'll need to compile a PHP engine into your iOS app and get Apple to allow it :) - Kodiak is able to do it because Apple allows exporting files written in their app, but Apple does not allow importing files into the app. Therefore, you must use copy/paste. The app is intended for writing and testing code not running a bunch of PHP script to create an app.
In short, the answer to your question is: Yes, it is possible to run PHP and write to an sqlite database in an iOS app. However, to do so you'll need to compile a PHP library into your app. Compiling PHP for iOS is no small feat (I haven't done it yet). Kodiak has done it, but I have not found a freely available PHP library for iOS yet.
I don't think you can "install" php on iOS. But, you may also, I can't tell.
What I would do, easier I think, is to create a local database with the same schema and stuff.
If offline, pull from the offline DB, online from the live DB (web).
How to ?
Write a script that will play the role of "Model". It's like in the MVC technic. Your script will only need to handle both DB. Since SQL is SQL, your queries will not really change so :
if(Offline){
// Offline
Use something similair to PDO but for SQLLite
} else {
// Online
Use something similair to PDO but for MySQL
}

Writing MS Access .MDF files with PHP

Is it possible to write .MDF files using PHP. I have a simple HTML table - x columns, y rows that I wish to convert into a MS Access dataset. There are no foreign keys or anything exotic, just a number of rows and columns.
I have Google searched but the only advice I seem to be able to find is to save it as a .CSV. Unfortunately this isn't acceptable for my client. They want to simply be able to double click the icon and have the database load up in Access (Seems you have to import csv files and can't just double click them).
Those are actually two different problems:
1) Parsing HTML to separate values
Since you seem to have found a way to export to csv, I won't be answering this. You already got your parser then.
2) Create an mdb file.
You can use odbc to talk to Access, that is, if you are in Windows. That means that PHP must be installed on a Windows machine. I'm not aware of any Linux drivers for Access..
You can use odbc_connect to connect to the database:
$conn = odbc_connect("dbname","" ,"");
From there, working with the database is pretty similar to MySQL as long as you use the ODBC functions.
The only problem is creating the actual MDB. This cannot be done in ODBC, you'll need the JET engine. It may be possible to create an MDB file from PHP, but it may be easier to create an empty MDB once, and copy that file from PHP whenever you need a new database.
Jackcess is a pure Java library for reading from and writing to MS Access databases. It is part of the OpenHMS project from Health Market Science, Inc. . It is not an application. There is no GUI. It's a library, intended for other developers to use to build Java applications.
http://jackcess.sourceforge.net/
But an MDF file is not a standard Access file extension. MDB and MDE are while MDW is for user level security.
Have not tested this but this might help you:
http://devzone.zend.com/article/4065
Note that active development of MDB Tools has moved from Sourceforge to Github
https://github.com/brianb/mdbtools

PDO Microsoft Access

I am running linux from bluehost and have PDO installed.
I wish to access *.mdb info, however I cannot seem to get past the connection string for PDO in this case.
$dbh = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb)};Dbq=$mdb;Uid=Admin");
Generates error:
SQLSTATE[IM002] SQLDriverConnect: 0 [unixODBC][Driver Manager]Data source name not found, and no default driver specified
I know the the path is correct:
/home1/[Removed]/pss/pss.md
Does anyone know of any solution to this issue using the PDO library?
As requested, expanding on my comment.
MDB support on Linux is limited; although MDB Tools (used by PDO on Linux) does provide partial read-only support, the general status seems far from production ready, as the FAQ says
1.2 What does MDB Tools do
MDB Tools is an open source suite of libraries and utilities to read
(and soon write) MDB database files.
1.6 Is there a roadmap?
Roughly, the plan moving forward looks like this.
Add index scan capability to libmdb and the SQL engine with a query
plan generator.
Add support for adding rows to existing tables and an mdb-import
tool.
Add full write support to libmdb and libmdbsql.
Extract queries, table properties, VBA script, forms.
mdb-check database consistancy checker and recovery tool.
Add joins.
Ability to add tables.
If you only need to read the .mdb, then installing mdbtools might be good enough.
If you need read-write, or any of the features on the roadmap, and must use .mdb, then use a Windows or possibly Mac host
If the .mdb is just a datasource, export it to SQL and use MySQL or PostGres (PostGres seems to have better ODBC support for Windows, if this is relevant to your project)

Extremely basic PHP and Mysql

Background: I am more of a designer than a programmer, but have hacked templates for many open source CMS's (Drupal, Joomla, Wordpress)
I want to start from scratch in regards to the relations of php and a mysql database.
Lets assume I have a working database and php engine locally.
What would be my first step to connecting to my database and creating a table... (im happy to be led to an appropriate tutorial...)
Many of the tutorials I have seen start with basic php, but I would rather explore the connection between the db and the php.
This seems to be a pretty good tutorial:
http://www.freewebmasterhelp.com/tutorials/phpmysql
W3Schools has a big tutorial on both PHP and MySQL.
A useful tip is also to know that you can look up anything you need on the official PHP and MySQL support pages by going to
php.net/search-term-here (example)
mysql.com/search-term-here (example)
If you want to really get a leg up... see if you can get programs like CPanel and PHPMyAdmin.
PHPMyAdmin will give you a more familiar UI for database control and ease you into using mySQL.
There are two ways: start to learn SQL and create tables trough SQL or use PHPMyAdmin to administer the database. From then on you only have to learn how to get the data from the database.
PhpMyAdmin also displays the queries so you can analyze these.
Get into PDO . It's gonna be the next big php/mysql thing. A lot of sites have switched to it. It's simple and securer than conventional mysql_* . A good book (for beginners and to start you going) that is for sure not for production is Wicked Cool Php Scripts
You can also have a look at XAMPP, which makes installing everything required for PHP/MySQL a breeze. Linux, Windows? No problem.
Nettuts and theme forest blog have a nice video series on learning php. They have other series such as wordpress and are currently doing a series on code ignitor which both use php.
Install mySQL and look up basic tutorials on how to create a table, update, etc. Once you feel handy with that, install XAMPP lite, where you can start practicing PHP. Look up tutorials on how to use PHP to access your database. XAMPP also has phpMyAdmin, so you have a much easier interface to work with your databases.
It'd be good if you had some basic programming knowledge too-- it makes understanding PHP significantly easier.
This is an example on how you connect php to your database:
<?php
$dbh = new PDO('mysql:host=localhost;dbname=test', 'root', '');
?>
mysql = database driver
host = localhost or 127.0.0.1
dbname = your database name
'root' = your database username
'' = database password
*Visit and explore this link for more info about PHP with Database.

PHP with SQL Server 2005+

Currently we have a hybrid ASP/PHP setup connecting to a SQL Server 2005 database. But all the query work is done on the client side, I'd like to move some of this to PHP.
What driver and/or connection string is needed to connect to Sql Svr and what is the syntax to use in PHP?
Update: OK so I was definitely trying to avoid using anything to do with copying DLLs etc. I'll look into the SQL2K5PHP driver (thanks Vincent). #jcarrascal for the sake of clarity, by "client side" I mean our application is an internal web app that runs as an HTA, with all queries done via javascript calls to an ASP which actually submits the DB request.
You have two options:
1) php_mssql extension : If you'd like something that has the same API mysql and mysqli has, then use the php_mssql extension. But there is a catch, the bundled ntwdblib.dll file with PHP is not working. You have to find this file from a SQL Server 2000 installation or you can find it on the Internet. This API is supposedly not very reliable but I have been using it without problem for about one year.
http://ca.php.net/mssql
2) Microsoft SQL Server 2005 PHP Driver : If you'd like something more modern but which does not have the same API and is missing some important functions (mssql_num_rows). The big plus is that it is supported by Microsoft and is likely to work with a future version.
http://msdn.microsoft.com/en-us/data/cc299381.aspx
Just use the mssql_connect() function like this:
$conn = mssql_connect('localhost', 'sa' , '123456')
or die('Can\'t connect.');
mssql_select_db('database', $conn)
or die('Can\'t select the database');
Functions relating to SQL Server are defined in the PHP manual for the MSSQL driver.
One question though, "all the query work is done on the client side" WTF? :D
PHP provides an extension for accessing Microsoft SQL Server databases. To use the SQL Server extension, all that is required is to activate the extension in the PHP configuration file.
Details on the MSDN page

Categories