<docbook><section><title>IODBCPHPHOWTOMacOSX</title><title> iODBC-PHP-Apache HOWTO for Mac OS X</title> iODBC-PHP-Apache HOWTO for Mac OS X
<bridgehead class="http://www.w3.org/1999/xhtml:h2">Disclaimer</bridgehead>
<para>The following is a HOWTO document for installing PHP with iODBC as an Apache module on <ulink url="MacOS">MacOS</ulink> X.
 Feel free to criticize, suggest modifications, or ask further questions.
 It is currently maintained by Tim Haynes of Openlink Software (iodbc@openlinksw.com)</para>
<para>Prerequisites include basic Unix familiarity, such as creating directories and users, using an editor, etc.</para>
<para>This HOWTO is intended to assist in connecting php/apache to back end databases via ODBC in a development environment and should not take the place of thorough testing before deployment on a production system.</para>
<bridgehead class="http://www.w3.org/1999/xhtml:h2"> ODBC Overview</bridgehead>
<para>ODBC (Open Database Connectivity) is an operating system- and database-independent communication API that allows a client application (productivity tool, other database, web page, custom application, etc.) to communicate via standards-based function calls to a back end database without relying on that vendor&#39;s proprietary communication protocols.</para>
<para>ODBC connections involve an application, driver manager, driver, and database.
 The driver manager under Microsoft Windows platforms is the ODBC Control Panel.
 The driver manager registers a set of ODBC driver connection parameters called a Data Source Name (DSN).
 An application looks to the driver manager for a DSN, and then passes the connection parameters specified in the DSN to the appropriate driver, which makes the connection.
Under non-Windows platforms you may need to install a Driver Manager.
 iODBC is an Open Source Driver Manager maintained by <ulink url="OpenLink">OpenLink</ulink> Software.
 It is released under a dual LGPL / BSD license.</para>
<bridgehead class="http://www.w3.org/1999/xhtml:h2"> Preface</bridgehead>
<para>You will also need an ODBC Driver and Database to complete the architecture.</para>
<para>If you need ODBC drivers to connect to a third-party database on the same or another machine, <ulink url="OpenLink">OpenLink</ulink> ODBC Drivers are available, and may be downloaded from <ulink url="http://www.openlinksw.com">http://www.openlinksw.com</ulink></para>
<para>The Virtuoso database may also be downloaded from <ulink url="http://virtuoso.openlinksw.com/">http://virtuoso.openlinksw.com/</ulink></para>
<para>Both Virtuoso and <ulink url="OpenLink">OpenLink</ulink>&#39;s Multi-Tier ODBC drivers download with a free 30-day demo license.</para>
<para>Support for setting up the <ulink url="OpenLink">OpenLink</ulink> Drivers may be obtained at <ulink url="http://support.openlinksw.com/">http://support.openlinksw.com/</ulink></para>
<bridgehead class="http://www.w3.org/1999/xhtml:h2"> Overview &amp; Requirements</bridgehead>
<para>First, download both php from <ulink url="http://www.php.net/">http://www.php.net/</ulink> and the iODBC driver manager from <ulink url="http://www.iodbc.org/.">http://www.iodbc.org/.</ulink> Ensure you have any other dependencies for PHP installed (for example, mysql, postgresql, imap, libxml etc).</para>
<para>If you download a <ulink url="MacOS">MacOS</ulink> X binary package of iODBC from the iodbc.org or <ulink url="OpenLink">OpenLink</ulink> websites, this will have been built as a framework.
 This is the recommended course of action, as it is compatible with other ODBC-using applications such as Excel, <ulink url="RealBasic">RealBasic</ulink>, other packages of PHP for <ulink url="MacOS">MacOS</ulink> X, etc.
 The majority of the rest of this HOWTO assumes you&#39;re building from source, should you want to customize the process yourself.</para>
<bridgehead class="http://www.w3.org/1999/xhtml:h3"> Building iODBC from source</bridgehead>
<para>If you are compiling from source, on Macos X, there are two ways whereby a package might be compiled - as traditional shared libraries, or as a `Framework&#39;.
 If you intend loading php from apache, then it must be compiled as a framework.
 It is also best if you compile iODBC as a framework as well.</para>
<para>Having downloaded the source tarball, unpack it, thus:</para>
<programlisting>[oplusmacosx:~/tim] openlink% tar xvfz libiodbc-3.51.1.tar.gz 
libiodbc-3.51.1
libiodbc-3.51.1/admin
libiodbc-3.51.1/admin/Makefile.am
libiodbc-3.51.1/admin/Makefile.in
libiodbc-3.51.1/admin/acinclude.m4
libiodbc-3.51.1/admin/config.guess
...
</programlisting><para> In the libiodbc-3.51.1 directory, read the README.MACOSX file.
 You will find both a . <emphasis>configure script and a mac</emphasis> directory: you can choose the former to build a shared library of iodbc, or the latter to make a framework, thus:</para>
<bridgehead class="http://www.w3.org/1999/xhtml:h4"> Traditional Shared-Library approach</bridgehead>
<programlisting>[oplusmacosx:~/libiodbc-3.51.1] openlink% ./configure   --enable-gui --prefix=$HOME/iodbc-3.51.1-shared
</programlisting><para> (enabling the GUI will require GTK to be installed) - then run `make&#39; and `sudo make install&#39;, or</para>
<bridgehead class="http://www.w3.org/1999/xhtml:h4"> <ulink url="MacOS">MacOS</ulink> X Framework approach</bridgehead>
<programlisting>[oplusmacosx:~/libiodbc-3.51.1-shared] openlink% cd mac
[oplusmacosx:~/libiodbc-3.51.1-shared/mac] openlink% make
sh framework-include.sh
cd iODBC; pbxbuild build -buildstyle Deployment
</programlisting><para> In the latter case, if you wish to install to a non-default location, you should amend Makefile in the mac/ subdirectory - search for the line beginning `install:&#39;, amend the DSTROOT directory and optionally remove the `sudo&#39; if you&#39;re installing it into your user&#39;s home directory:</para>
<programlisting>install:
cd iODBC;     pbxbuild install -buildstyle Deployment DSTROOT=$(HOME)/iodbc
cd iODBCinst; pbxbuild install -buildstyle Deployment DSTROOT=$(HOME)/iodbc
</programlisting><para> then you can run `make install&#39;.</para>
<bridgehead class="http://www.w3.org/1999/xhtml:h2"> Building PHP with iODBC</bridgehead>
<para>Obtain and unpack the sources for php, e.g.
 with:</para>
<programlisting>[oplusmacosx:~/tim] openlink% bzcat &lt; php-4.3.3.tar.bz2 | tar xvf -
php-4.3.3
php-4.3.3/build
php-4.3.3/build/build.mk
php-4.3.3/build/build2.mk
php-4.3.3/build/buildcheck.sh
php-4.3.3/build/config-stubs
...
</programlisting><para> If you built iODBC as a shared library earlier, building PHP against it is a simple matter of running configure with a point to the top directory where iODBC was installed:</para>
<programlisting>[oplusmacosx:~/php-4.3.3] openlink% ./configure --with-iodbc=$HOME/iodbc-shared &amp;&amp; nice make
</programlisting><para> However, if you built iODBC as a framework, it&#39;s a bit more complicated.
PHP has no support for iODBC as a framework out of the box, so you need to configure it first and then edit the generated Makefile.
 A complete recommended set of configure options for PHP is:</para>
<programlisting>./configure \
    --prefix=/Library/PHP4  \
    --with-openssl=/usr  \
    --with-zlib=/usr  \
    --enable-calendar  \
    --enable-exif \
    --enable-ftp  \
    --enable-mbstring  \
    --disable-mbregex  \
    --enable-session \
    --enable-sockets  \
    --enable-tokenizer  \
    --enable-wddx  \
    --with-xml  \
    --enable-yp \
    --enable-versioning  \
    --enable-trans-id  \
    --enable-track-vars  \
    --with-mysql \
    --with-ldap=/usr  \
    --with-iodbc=/usr  \
    --with-jpeg-dir=/Library/PHP4 \
    --with-png-dir=/Library/PHP4  \
    --enable-xslt  \
    --with-xslt-sablot=/Library/PHP4 \
    --with-pdflib=/Library/PHP4  \
    --with-tiff-dir=/Library/PHP4 \
    --with-pgsql=/Library/PHP4  \
    --with-iconv=/Library/PHP4 \
    --with-sybase=/Library/PHP4  \
    --with-mcrypt=/Library/PHP4 \
    --with-curl=/Library/PHP4  \
    --with-freetype-dir=/Library/PHP4 \
    --with-ttf=/Library/PHP4  \
    --with-xmlrpc  \
    --enable-mime-magic  \
    --without-pspell \
    --with-expat-dir=/Library/Apache2  \
    --with-t1lib=/Library/PHP4  \
    --enable-dbx \
    --without-pcre-regex  \
    --without-pgsql  \
    --enable-shared  \
    --enable-cli \
    #    --with-apxs2=/Library/Apache2/bin/apxs 
</programlisting><para> Note that if you enable the cli (as would make sense for testing) you should disable support for apxs, otherwise duplicate symbols may result - chose one or the other.</para>
<para>Now, edit the Makefile, and search for references to iodbc.
 You need to replace these lines:</para>
<programlisting>ODBC_INCLUDE = -framework iODBC -F /Users/openlink/iodbc-mac/Library/Frameworks/
ODBC_LIBS =  -framework iODBC -F /Users/openlink/iodbc-mac/Library/Frameworks/
ODBC_LFLAGS = -framework iODBC -F /Users/openlink/iodbc-mac/Library/Frameworks/
</programlisting><para> Remove the first two words on the right side of this line:</para>
<programlisting>EXTRA_LDFLAGS = -export-symbols \
/Users/openlink/php-4.3.3/sapi/apache2handler/php.sym \
-L/Library/PHP4/lib \
-L/Users/openlink/iodbc-mac//Library/Frameworks/iODBC.framework/lib \
-L/Library/Apache2/lib
</programlisting><para> (the `-export-symbols&#39; and path to `php.sym&#39;)</para>
<para>Replace the EXTRA_LDFLAGS line accordingly:</para>
<programlisting>EXTRA_LIBS = -lsablot -lexpat -lexpat -lexpat -lsybdb -lpdf -lz \
-ltiff -lpng -ljpeg -liodbc -lmcrypt -lltdl -lldap -llber -liconv -lcurl \
-lz -lssl -lcrypto -lm -ldl -lcurl -lz -lssl -lcrypto -lz -liodbc
EXTRA_LDFLAGS = -L/Library/PHP4/lib \
-L/Users/openlink/iodbc-mac//Library/Frameworks/iODBC.framework/lib \
-L/Library/Apache2/lib
EXTRA_LIBS = -lsablot -lexpat -lexpat -lexpat -lsybdb -lpdf -lz -ltiff \
-lpng -ljpeg -lmcrypt -lltdl -lldap -llber -liconv -lcurl -lz -lssl \
-lcrypto -lm -ldl -lcurl -lz -lssl -lcrypto -lz -framework iODBC
</programlisting><para> Finally, ensure there are no further references to `-liodbc&#39; in the Makefile, before typing `make&#39;.</para>
<bridgehead class="http://www.w3.org/1999/xhtml:h3"> Testing a PHP CLI works</bridgehead>
<para>It&#39;s advisable to build PHP as a CLI before going the whole way and testing with apache.
 To test that PHP is working with iODBC you can do something like:</para>
<programlisting>[oplusmacosx:~/php-4.3.3] openlink% echo &#39;&lt;?php echo &quot;foo
&quot;; odbc_connect(&quot;foo&quot;,&quot;username&quot;,&quot;pwd&quot;);?&gt;&#39; | ./sapi/cgi/php
Content-type: text/html
X-Powered-By: PHP/4.3.3
foo
&lt;br /&gt;
&lt;b&gt;Warning&lt;/b&gt;:  odbc_connect(): SQL error: [iODBC][Driver Manager]Data source name not found and no default driver specified. Driver could not be loaded, SQL state IM002 in SQLConnect in &lt;b&gt;/Users/openlink/php-4.3.3/-&lt;/b&gt; on line &lt;b&gt;1&lt;/b&gt;&lt;br /&gt;
</programlisting><para> The fact that the DSN `foo&#39; is not found is immaterial; what matters here is that it&#39;s iODBC emitting a standard error message, showing PHP and iODBC are talking together.
 (If iODBC had not been included, you would have an error message about an attempt to call an undefined function `odbc_connect&#39;, instead).</para>
<para>You can double-check that the framework version of iODBC is in use by running the above command through `otool -l&#39; and grep-ing the output for iodbc.
 If you see, for example, /usr/lib/libiodbc in there, you know it&#39;s using the shared-library version instead.</para>
<programlisting>[oplusmacosx:~/php-4.3.3-shared-usr] openlink% echo \
&#39;&lt;?php echo &quot;foo
&quot;; odbc_connect(&quot;foo&quot;,&quot;username&quot;,&quot;pwd&quot;);?&gt;&#39; \
| otool -l ./sapi/cgi/php | grep -i iodbc
name /usr/lib/libiodbc.2.1.6.dylib (offset 24)
</programlisting><para> </para>
<bridgehead class="http://www.w3.org/1999/xhtml:h2"> Integrating with Apache</bridgehead>
<para>If you&#39;ve used the CLI mode as a test above, you will need to reconfigure and rebuild PHP using --disable-cli and --with-apxs2=/path/to/apxs.</para>
<para>Once PHP is built with all your required configure options against a framework iODBC, it should be sufficient to execute `make install&#39; for it to invoke apxs, which will place suitable directives in apache&#39;s httpd.conf:</para>
<programlisting># PHP4 configuration
LoadModule php4_module modules/libphp4.so
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
</programlisting><para> You should restart the apache webserver when these directives are in place, and run a simple test-script to see how PHP is being loaded, and attempt a simple database connection:</para>
<para>phpinfo.php:</para>
<programlisting>&lt;?php phpinfo(); ?&gt;
</programlisting><para> and...</para>
<programlisting>&lt;?php

#Set up environment variables
putenv(&quot;DYLD_LIBRARY_PATH=/path/to/odbcsdk/lib&quot;);
putenv(&quot;ODBCINSTINI=/path/to/odbcinst.ini&quot;);
putenv(&quot;ODBCINI=/path/to/odbc.ini&quot;); //odbc.ini contains your DSNs.

$dsn=&quot;DSN_NAME&quot;; 
$user=&quot;username&quot;;
$password=&quot;password&quot;;

$sql=&quot;SELECT * FROM table&quot;;

// directly execute mode
if ($conn_id=odbc_connect(&quot;$dsn&quot;,&quot;&quot;,&quot;&quot;)) {
echo &quot;connected to DSN: $dsn&quot;;
if($result=odbc_do($conn_id, $sql)) {
echo &quot;executing &#39;$sql&#39;&quot;;
echo &quot;Results: &quot;;
odbc_result_all($result);
echo &quot;freeing result&quot;;
odbc_free_result($result);
}else{
echo &quot;can not execute &#39;$sql&#39; &quot;;
}
echo &quot;closing connection $conn_id&quot;;
odbc_close($conn_id);
}
else{
echo &quot;can not connect to DSN: $dsn &quot;;
}
?&gt;
</programlisting><para> </para>
<bridgehead class="http://www.w3.org/1999/xhtml:h2"> References</bridgehead>
<itemizedlist mark="bullet" spacing="compact"><listitem><ulink url="MacOS">MacOS</ulink> X PHP  <ulink url="http://www.php.net/manual/en/install.macosx.php">installation instructions</ulink> </listitem>
<listitem>phpmac.com  <ulink url="http://www.phpmac.com/">site deditcated to PHP on the Mac</ulink> </listitem>
<listitem><ulink url="http://www.iodbc.org">http://www.iodbc.org</ulink> </listitem>
<listitem>History of  <ulink url="ODBCOnUnix">ODBC on Unix</ulink> </listitem>
<listitem><ulink url="http://www.php.net/support.php">PHP main site</ulink> </listitem>
<listitem><ulink url="http://www.openlinksw.com/">OpenLink Software</ulink> -  <ulink url="http://www.openlinksw.com/support/suppindx.htm">Support</ulink> ,  <ulink url="http://www.openlinksw.com/support/teclinks.htm">links</ulink> and  <ulink url="http://www.openlinksw.com/info/docs/odbcwhp/tableof.htm">white-papers</ulink></listitem>
</itemizedlist></section></docbook>