ADODB with PHP and oracle

Oracle is the popular commercial database which is being used with the PHP.There are vaious ways to connect to oracle database.ADODB library is one of the fastest way to connect php with the oracle database.It has Has multi-tier design. Simple high-level design for beginners, and also lower-level advanced Oracle functionality.ADODB provide very high speed databse connectivity with catching and fastest database abstaction.It also allow multiple prepare statement.

An example to connect php with oracle using ADODB library is as follows.

include"/path/to/adodb.inc.php";
$db = NewADOConnection("oci8");
$db->Connect($tnsName, "scott", "tiger");

$rs = $db->Execute("select * from emp where empno>:emp order by empno",
array('emp' => 7900));
while ($arr = $rs->FetchRow())
{
print_r($arr);

}
The Execute( ) function returns a recordset object, and you can retrieve the rows returned using $recordset->FetchRow( ).
You can also query the database using the standard Microsoft ADO MoveNext( ) metaphor. The data array for the current row is stored in the fields property of the recordset object, $rs. MoveNext( ) offers the highest performance among all the techniques for iterating through a recordset:

$rs = $db->Execute("select * from emp where empno>:emp", array('emp' => 7900));
while (!$rs->EOF) {
print_r($rs->fields);
$rs->MoveNext();
}

For easy pagination support, we provide the SelectLimit function. The following will perform a select query, limiting it to 100 rows, starting from row 201 (row 1 being the 1st row):

$offset = 200; $limitrows = 100;
$rs = $db->SelectLimit('select * from table', $limitrows, $offset);

Caching

You can define a database cache directory using $ADODB_CACHE_DIR, and cache the results of frequently used queries that rarely change. This is particularly useful for SQL with complex where clauses and group-by's and order-by's. It is also good for relieving heavily-loaded database servers.The following example will cache the following select statement for 7200 seconds

$ADODB_CACHE_DIR = '/var/tmp';
$rs = $db->CacheExecute(7200, "select names from allcountries order by 1");
Using Prepare statements

Prepare( ) is for compiling frequently used SQL statement for reuse. For example, suppose we have a large array which needs to be inserted into an Oracle database. The following will result in a massive speedup in query execution (at least 20-40%), as the SQL statement only needs to be compiled once:


Domain Model Validation

Using some magic methods can integrate validation without having to implement get and set methods everywhere.


class ValidationException extends Exception {
}

abstract class DomainModel {
private $validationErrors = array();

final public function __get($field) {
$getMethodName = 'get' . ucfirst($field);
if (method_exists($this, $getMethodName)) {
return call_user_func(array($this, $getMethodName));
} elseif (isset($this->$field)) {
return $this->$field;
}
}

final public function __set($field, $value) {
$setMethodName = 'set' . ucfirst($field);
if (array_key_exists($field, $this->validationErrors)) {
unset($this->validationErrors[$field]);
}
if (method_exists($this, $setMethodName)) {
try {
call_user_func(array($this, $setMethodName), $value);
} catch (ValidationException $e) {
$this->validationErrors[$field] = $e->getMessage();
}
} elseif (array_key_exists($field, get_object_vars($this))) {
$this->$field = $value;
}
}

final public function isValid() {
return count($this->validationErrors) == 0;
}

final public function getValidationErrors() {
return array_values($this->validationErrors);
}

final public function setData($data) {
foreach ($data as $field => $value) {
$this->__set($field, $value);
}
}

final public function getData() {
return get_object_vars($this);
}
}

class Test extends DomainModel {
protected $message;

public function setMessage($msg) {
if ($msg == null || strlen(trim($msg)) == 0) {
throw new ValidationException("Message must not be empty");
}
$this->message = $msg;
}
}

$obj = new Test();
$obj->message = "Hello!";
echo $obj->message . "\n";
$obj->message = " ";
if (!$obj->isValid()) {
foreach ($obj->getValidationErrors() as $error) {
echo $error . "\n";
}
}

What is CakePHP? Why Use it?

http://book.cakephp.org/2.0/en/plugins.html

CakePHP is a free, open-source, rapid development framework for PHP. It’s a foundational structure for programmers to create web applications. Our primary goal is to enable you to work in a structured and rapid manner–without loss of flexibility.

CakePHP takes the monotony out of web development. We provide you with all the tools you need to get started coding what you really need to get done: the logic specific to your application. Instead of reinventing the wheel every time you sit down to a new project, check out a copy of CakePHP and get started with the real guts of your application.

CakePHP has an active developer team and community, bringing great value to the project. In addition to keeping you from wheel-reinventing, using CakePHP means your application’s core is well tested and is being constantly improved.

Here’s a quick list of features you’ll enjoy when using CakePHP:

Active, friendly community
Flexible licensing
Compatible with versions PHP 5.2.6 and greater.
Integrated CRUD for database interaction.
Application scaffolding.
Code generation.
MVC architecture.
Request dispatcher with clean, custom URLs and routes.
Built-in validation.
Fast and flexible templating (PHP syntax, with helpers).
View Helpers for AJAX, JavaScript, HTML Forms and more.
Email, Cookie, Security, Session, and Request Handling Components.
Flexible ACL.
Data Sanitization.
Flexible Caching.
Localization.
Works from any web site directory, with

PHP assignment operators

PHP assignment operators are used to assign value in a variable such that we can assign value 10 in a variable $a like that $a=10. Now the value of $a is 10 because we assign 10 in variable a.

equal (=) sign is used to represent PHP assignment operator but in array we use => sign to assign value. Now take a look at an example of PHP assignment operators.
<?php
$a = 20;
echo $a;
?>
Save and run the above code.
We can use PHP arithmetic operator in PHP assignment operator. Here is an example:
<?php
$a=10;
$a+=10;
echo $a;
?>