How PHP Connect to Oracle: Oracle Connection String
November 18th, 2008 by it gossips | Comments | Filed in ProgrammingDo you had a problem connecting PHP with oracle database? Then maybe this will solve your problem. This is a code snippet I copy from PHP Developer Guide for Oracle. You can buy Oracle books at ebay.
If in the previous post there is a way to unlock Oracle system account. Then this time I want to share how you can connect to database using PHP.
To form a database connection in your PHP application, you use the oci_connect() function with three string parameters:
$conn = oci_connect($username, $password, $db)
<?php // File: anyco.php
require('anyco_ui.inc');
// Create a database connection
$conn = oci_connect('hr', 'hr', '//localhost/XE');
ui_print_header('Departments');
do_query($conn, 'SELECT * FROM DEPARTMENTS');
ui_print_footer(date('Y-m-d H:i:s'));
// Execute query and display results
function do_query($conn, $query)
{
$stid = oci_parse($conn, $query);
$r = oci_execute($stid, OCI_DEFAULT);
print '<table border="1">';
while ($row = oci_fetch_array($stid, OCI_RETURN_NULLS)) {
print '<tr>';
foreach ($row as $item) {
print '<td>'.
($item ? htmlentities($item) : ' ').'</td>';
}
print '</tr>';
}
print '</table>';
}
?>
Worth Reading:
- Spread your database connections with PHP PDO
- Put some MVC in your PHP
- CodeSOD: Keep it Simple and Stupid

Tags: CodeSOD, Languages, Oracle, Oracle Database, php, Programming, Select, SQL
Subscribe Feed (RSS)





























































