<?php
/**
* Oracle wrapper for PHP class TEST.
* This is example script!!!
* To make this script work you have to change connection parameters and query string.
* @author Tarmo Protsin <tarmopr@hot.ee>
*/
// Connection string (this should be in your configuration file or something ;).
$cstr = '(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP) (HOST =127.0.0.1)(PORT = 1521)) (CONNECT_DATA= (SID = testdb)))/username/password'; // sid/user/password
// Connection array (spliting connection string values into array by '/' character).
$carr = split('/', $cstr);
// Include oracle class file.
include('Oracle.lib.php');
/**
* New database connection (with connection parameters).
* If it's to confusing for you, you can also connect like:
* <code>
* $Sid = '(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP) (HOST =127.0.0.1)(PORT = 1521)) (CONNECT_DATA= (SID = testdb)))';
* $Username = 'username';
* $Password = 'password';
*
* $dbc = new Oracle();
* $dbc->connect($Sid, $Username, $Password);
* </code>
*/
$dbc = new Oracle($carr);
// Set debug mode for more information about query.
$dbc->Debug = 1;
/**
* Example 1:
* Insert data into table users and get last inserted identificator value.
*/
// Insert query clause.
$sql = "
INSERT INTO
USERS(FIRSTNAME, LASTNAME, USERNAME[, ...])
VALUES ( 'blaa', 'blaa', 'blaa'[, ...])
";
// Check if query failed.
// If failed then print out the error and abort script execution.
if (!$dbc->query($sql))
{
print "Error: Could not insert User data into DataBase!<br>\n";
print "Oracle error: " . $dbc->Error . "!<br>\n";
die();
}
// Get last inserted id.
// You have to have sequence named USERS_SEQ for table USERS.
$sql = 'SELECT USERS_SEQ.CURRVAL AS NEW_ID FROM DUAL';
if ( $dbc->query($sql) && $dbc->next_record() )
$last_inserted_id = $dbc->f('NEW_ID'); // return squence's current value (last inserted id for table USERS)
// Print some feedback.
print "Query successfully executed! <br>\n'";
print "Inserted User's ID #" . $last_inserted_id . "!<br>\n";
/**
* Example 2:
* Select all fields and rows from table users and get the result into array.
*/
// Select clause.
$sql = "SELECT * FROM USERS";
// Check if query failed.
// If failed then print out the error and abort script execution.
if (!$dbc->query($sql))
{
print "Error: Could not get User data from DataBase!<br>\n";
print "Oracle error: " . $dbc->Error . "!<br>\n";
die();
}
// Get result into array by using while cycle.
$result_arr = array();
while ($dbc->next_record())
{
$result_arr[] = $dbc->Record;
}
// Get result into array by using the method build_results().
$result_arr = $dbc->build_results($sql);
// Output the result array by using the print_r function of PHP.
// print_r -- Prints human-readable information about a variable (PHP 4)
print '<pre>';
prin_r($result_arr);
print '</pre>';
/**
* 2007-07-26
* Example 3:
* Example of using insert and update functions.
*/
// Column values in array: 'column_name' => 'column_value'
$columnsValues = array('name' => 'new name');
// Insert column values into table named 'news'.
$dbc->insert('news', $columnsValues);
// Update column values in table named 'news'.
// Update values in row with identificator column named 'id' and value of '4'
$dbc->update('news', 'id', $columnsValues, '4');
/**
* 2007-07-26
* Example 4:
* Example of using the transactions.
*/
// Start new transaction.
$dbc->startTransaction();
// Column values in array.
$columnsValues = array('name' => 'new name');
// Insert values into table named 'news'.
// If the query failes rollack the transaction, commit otherwise.
if (!$dbc->insert('news', $columnsValues))
{
$dbc->rollback();
}
else
{
$dbc->commit();
}
?>
|