PHP with SQL Server 2005+ - php

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

Related

How can I connect to an HSQL database using PHP?

I would like to manipulate an HSQL database using PHP. I have read the documentation regarding HyperSQL and it only shows an example of connecting using Java. How can I connect to an HSQL database in PHP?
You can use the ODBC driver for HSQLDB. The installer is available here.

Converting Oracle code to MySQL

If I have a .php file with some Oracle code previously accessing a Oracle DB, but have setup a MySQL DB and want to edit the existing .php file to be compatible with the MySQL DB, how would I go about doing this? Do I have to manually go through each line and search for the equivalent MySQL statement, and if so, is there a library I can access that gives me the Oracle to MySQL equivalent command?
There is this website with a good compilation of tips and tools to help you out on this task: http://www.sqlines.com/oracle-to-mysql

PHP string on Host server from DW CS6

I am new to PHP and teaching myself. I have built some training sites but I am having a problem when I load the files from Dreamweaver 6 to my internet hosting server.
The site works and displays fine locally. But when I load the site files on the hosting server and try to view the site it gives me an error coming from my server strings. If I edit the code on the server file(see below) I can make it work fine.
Is there a setting on DW that I need to do to make it compatible and not have to be edited once I put the files to the host server? Or is it a version problem? I am using DW CS6 and PHP version 5.4.3
My hosting server is using PHP 5.3.
Here what the code looks like....
Existing code that comes from Dreamweaver - "mysql_select_db($database_ique22, $ique22)";
On server if I edit to the following it clears the error - "mysql_select_db($database_ique22)";
Thanks for any advice.....
I'm not familiar with what Dreamweaver implements as far as helping with PHP, but if you're really interested in learning PHP I'd ditch Dreamweaver altogether. The PHP.net docs are fantastic and an editor like Sublime Text 2 will work much better for you without doing confusing things.
In any case, the second parameter of mysql_select_db is the link identifier (used generally if you need to connect to multiple MySQL servers). It should only throw an error if $ique22 is not set to a MySQL connection using something along the lines of $ique22 = mysql_connect("server","user","pass");
One thing to keep in mind is that the mysql_ function set is deprecated and you'll want to switch to using mysqli.
http://php.net/manual/en/book.mysqli.php

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

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!

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)

Categories