RE: [PHP] Oracle and PHP

From: Date: Tue, 26 Jun 2001 23:43:27 +0000
Subject: RE: [PHP] Oracle and PHP
Groups: php.db php.general 
Request: Send a blank email to php-db+get-9852@lists.php.net to get a copy of this message
> I'm a newbie in PHP, what should I do to connect to Oracle Database. > Do I have to install a library to do that? > Please anyone, help. First things first: I've got PHP running with Oracle 8.1.5 here. Any other version may be slightly different (ta, Oracle!) ... To have PHP talk to an Oracle database, you need to compile PHP against Oracle's client libraries. This can only be accomplished if Oracle is installed on the machine that will run PHP(*). The exact files needed can be found in $ORACLE_HOME/product/8.1.5/lib/libclntsh.so.* - if PHP can't find these, it can't compile(+). NOTE: If you manage to compile PHP against Oracle successfully, but notice that when you start Apache it silently fails, you have encountered a problem that's plagued a few users - certain compiler versions have a bug that causes this all to fall in a great big heap, and you will have to read around the PHP General archives to find out how to fix it (this is why we have sysadmins ;)). Once installed and running happily, you'll need to know what database you want to connect to, and place it in product/8.1.5/network/admin/tnsnames.ora. The general format of this file is: ========== SOMETHING = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = host.the.db.is.on)(PORT = 1688)) ) (CONNECT_DATA = (SID = SOMETHING) ) ) ========== Right. Now we have enough to connect to the server. Your PHP code to connect will be: <? $oraclesid = "SOMETHING"; $oracleusername = "username"; $oraclepassword = "password"; PutEnv("ORACLE_SID=".$oraclesid); PutEnv("TWO_TASK=".$oraclesid); PutEnv("ORACLE_HOME=/home/oracle/product/8.1.5"); // This is the Oracle home directory on your system if ($oracle = OCIPLogon($oracleusername, $oraclepassword, $oraclesid)) { echo "$oracle ".OCIServerVersion($oracle)."<BR>\n"; } else { echo "Couldn't connect to Oracle.<BR>\n"; Exit(); } ?> Now, $oracle is your Connection Resource. In order to execute some SQL on that database, try this: <? $sql = "SELECT * FROM tablename"; // Parse the SQL to turn it into a statement $statement = OCIParse($oracle, $sql); // Execute it on the server $execute = OCIExecute($statement); // Now, rather than accessing $execute (as you might expect) for // the results, we access $statement still. // Find the number of columns returned $numberofcolumns = OCINumCols($statement); // Now you'll want to loop through the returned data while( OCIFetch($statement) ) { $data = ""; // Now, loop through your columns for ( $j = 1; $j <= $ncols; $j++ ) { $colname = OCIColumnName($statement, $j); $colvalue = OCIResult($statement, $j); $data[$colname] = "value"; } $result[] = $data; } ?> Now, count($result) is the total number of of rows. $result is an array of rows. Each row is an array, $result[rownumber][key] = $value (*) You'll pretty much have to do a full install to get these files. Or, you can drag them kicking and screaming over from another system that already has Oracle installed. (+) It's true that you need Oracle installed to compile PHP, but if you compile PHP statically, you can then get rid of the libraries and strip Oracle back to product/8.1.5/network/admin/tnsnames.ora. The tradeoff is your PHP module will end up rather large(!), and it'll thus take longer to start Apache. Hope this helps you out a little. If anyone wants to correct me or add stuff, please let me know (and CC me on the mail please if you're on PHP-DB, since I'm not). Jason -- Jason Murray jasonm@melbourneit.com.au Web Developer, Melbourne IT "What'll Scorpy use wormhole technology for?" 'Faster pizza delivery.'

« previous php.db (#9852) next »