Thursday, July 31, 2008

Connecting to Oracle using Pear MDB2

I am currently working on a PHP based website that needs to connect to an Oracle database. In order to connect to the database I decided to use Pear mdb2. I figured this would help if I ever needed to move to another type of database.

I have used Pear before a few times and never seemed to have much of a problem with it, however this time I was struggling to even get connected. I believe the main problem was that I was connecting to an Oracle database instead of a MySQL database (which I am more comfortable with). After a long time searching I was able to find a solution to the problem. Below is my solution that I used to connect to the Oracle database. I found a few examples of it online but it was not easy to find, hopefully this will help someone out.

/**** Code Starts Here *****/
//the connect function returns the database connection object
function connect(){

$db_user = 'username';
$db_pass = 'password';
$db_host = 'host';
$db_port = 'port_number';
$sid = 'database_name';
$type = 'oci8';

//Create a host string that tells oracle to look for
//the database name (SID) instead of the service
$host = "(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)
(HOST = $db_host)
(PORT = $db_port)))
(CONNECT_DATA =
(SID = $sid)))";

$dsn = "$type://$db_user:$db_pass@$host";

$options = array( 'emulate_database' => false );
$mdb2 =& MDB2::connect($dsn, $options);
if(PEAR::isError($mdb2)) {
die("Error while connecting : " . $mdb2->getMessage().
" **-UserInfo-** ". $mdb2->getUserinfo());
}

return $mdb2; //return the database connection object
}

?>
/**** Code Ends Here ****/

The main struggle while trying to connect was putting together the host connection string. Now that I am able to connect I have been working on calling an Oracle Stored Procedure from PEAR MDB2. So far it is proving to be a difficult task to figure out, I will post back any results. If you have any questions feel free to leave them in the comments!

Monday, July 28, 2008

Introduction

Hello, to any possible readers... This is my first blog post! I am currently a software engineering intern contracted to the government. I work at a medium sized government facility in Sioux Falls, South Dakota. I currently am in my final year at Dakota State University, majoring in Computer Information Systems (with minors in Computer Science, and Business Administration). Some of the technologies I may talk about include Java, PHP, Ajax, CSS, JavaScript, Perl, XML/XSL/XSLT, C/C++, ASP.Net, as well as various others. I plan on using this to blog about problems I encounter and ways that I was able to resolve these problems. I am doing this in hopes to save someone time in the future (everyone knows time is important). I figure that if I have to spend countless hours struggling to find a solution to a problem, I might as well let someone else know about it. This way if they ever encounter the same problem they will not have to spend as much time searching for a solution.

Outside of programming/developing I enjoy playing guitar and spending time with friends and family.

I do not claim to be an expert on any of the technologies I listed earlier, nor do I clam that my posts will work for everyone or be 100% accurate. I am just hoping to help a few people out...