 |
|
ss
Oracle Tips by Burleson |
Extract
extract -- Import variables into the current
symbol table from an array
Description
This function is used to import variables from an
array into the current symbol table. It takes an associative array,
var_array, and treats keys as
variable names and values as variable values. For each key/value pair,
it will create a variable in the current symbol table, subject to the
extract_type and
prefix
parameters.
Note: Beginning with version 4.0.5, this function
returns the number of variables extracted.
: EXTR_IF_EXISTS and
EXTR_PREFIX_IF_EXISTS were introduced in version 4.2.0.
: EXTR_REFS was introduced in version 4.3.0.
extract() checks each key to see whether it
has a valid variable name. It also checks for collisions with existing
variables in the symbol table. The way invalid/numeric keys and
collisions are treated is determined by the
extract_type. It can be one of the following values:
-
extr_overwrite - If there is a
collision, overwrite the existing variable.
-
extr_skip - If there is a
collision, do not overwrite the existing variable.
-
extr_prefix_same
- If there is a collision, prefix the variable name with
prefix.
-
extr_prefix_all
- Prefix all variable names with prefix. Beginning with PHP
4.0.5, this includes numeric variables as well.
-
extr_prefix_invalid
- Only prefix invalid/numeric variable names with prefix. This
flag was added in PHP 4.0.5.
-
extr_if_exists - Only
overwrite the variable if it already exists in the current symbol
table, otherwise do nothing. This is useful for defining a list of
valid variables and then extracting only those variables that are
defined out of $_REQUEST, for example. This flag was added in PHP
4.2.0.
-
extr_prefix_if_exists - Only create prefixed variable names if
the non-prefixed version of the same variable exists in the current
symbol table. This flag was added in PHP 4.2.0.
-
extr_refs - Extracts
variables as references; this effectively means that the values of the
imported variables are still referencing the values of the var_array
parameter. Use this flag on its own or combine it with any other flag
by OR’ing the extract_type. This flag was added in PHP 4.3.0.
If extract_type
is not specified, it is assumed to be extr_overwrite. Note that
prefix is only required
if
extract_type is EXTR_PREFIX_SAME, extr_prefix_all,
extr_prefix_invalidor
extr_prefix_if_exists.
If the prefixed result is not a valid variable
name, it is not imported into the symbol table. extract()
returns the number of variables successfully imported into the symbol
table.
Extract
functions can be used to extract
variables from global arrays like $_SESSION or $_POST into program
variables, thus achieving almost the same effect as with the
register_globals configuration directive. Of course, the negative
consequences are also the same. Collisions with existing variables can
and will occur. This function is to be used with extreme caution, if
ever. Debugging scripts with problems like this can be extremely
painful.
The last question that needs to be answered before
the next example is “what can be put into the $_SESSION array”?
The answer is anything, provided the type is
defined before executing the session_start() function. Object
types like OCI_Session are ideal for sharing via session
mechanism. The object and all of its members are shared automatically,
without having to put each member into the $_SESSION array.
The last request does somewhat limit the
usefulness of the __autoload() function and
requires each class description be loaded by an explicit include, not
waiting for PHP to load it when a new object is created.
It is now possible to rework example 13, and do
something useful with the newly created database connection. Login
form and the OCI_Session will remain the same; the only part
that needs changing is the example13.php script itself:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML
3.2//EN">
<html>
<head>
<title>Example 13a</title>
</head>
<body>
<?php
require_once('OCI_Session.php');
require('login_form.php');
session_start();
if (!isset($_POST['user'])) {
login_form('SCOTT');
}
else {
try { $dbh=new
OCI_Session($_POST['user'],
$_POST['passwd'],
$_POST['database']);
if (isset($dbh->err))
throw new exception($dbh->err['message']);
else {
$_SESSION['dbh']=$dbh;
header('Location: query1.php');
}
}
catch (exception $e) {
?>
<center>
<?php
echo "Exception:".$e->getMessage();
login_form($_POST['user']);
?>
<br>
</center>
<?php } }?>
</body>
</html>
The important changes
are related to the session and marked by the bold font. Note that
session_start() was called after including the OCI_Session
and that the __autoload()
function was not used. The next thing to note is the use of the
header() function to redirect browser to another URL. In this case
it was just “query1.php” which is a short form for:
http://localhost/work/query1.php
This is another PHP
script on the local machine. It is preferable to type the full URL
because it will work the same way, regardless of the server and
virtual directory configuration. Header function sends the argument
as a part of HTTP header to the browser. This is a little trick to
“redirect” the client browser to another script.
The goal of this
section was to demonstrate sharing of data between sessions. To do
that, another script named query1.php is needed:
<html>
<head>
<title>Query1</title>
</head>
<body>
<?php
require_once('OCI_Session.php');
session_start();
$dbh=$_SESSION['dbh'];
$SQL="select
e.ename,e.empno,d.dname,d.loc,e.sal
from emp e, dept d
where e.deptno=d.deptno
order by e.deptno,ename";
try {
$dbh->refresh();
if (!$dbh->db) {
$e=$dbh->err;
throw new Exception ('CONN:'.$e['message']);
}
$sth=oci_parse($dbh->db,$SQL);
if (!$sth) {
$e=oci_error($sth);
throw new Exception ('PARSE:'.$e['message']);
}
if (!oci_execute($sth)) {
$e=oci_error($sth);
throw new Exception ('EXEC:'.$e['message']);
}
?>
<center>
<table>
<tr align="left"
valign="middle"
style="background-color: #ADD8E6; ">
<td>ENAME </td>
<th>EMPNO </th>
<th>DPT. NAME </th>
<th>LOCATION </th>
<th>SALARY </th>
</tr>
<?php
while ($row=oci_fetch_array($sth)) {
?>
<tr>
<td> <?=$row[0]?> </td>
<td> <?=$row[1]?> </td>
<td> <?=$row[2]?> </td>
<td> <?=$row[3]?> </td>
<td> <?=$row[4]?> </td>
</tr>
<?php
}
?>
</table>
</center>
<?php
}
catch (Exception $e) {
print $e->getMessage();
}
?>
</body>
<html>
What does this script do? This script starts a
session and reads all session variables. There is only one, the
variable $dbh. It is a database handle, shared with the login
form drawn by the example13a.php.
It is important to note that refresh() is
being called on the handle. Remember from the beginning of the
chapter, the refresh() method of the OCI_Session class
re-establishes the database connection.
Why re-connect to the database? This is not the
same request as the one that handled the login form, so it may be
handled by a different process. The entities that connect to
databases are processes, not “pages” or browsers. Thus, it must be
assumed that this new process is not connected to the database. This
means the user must re-connect. This is the primary purpose of the
refresh() member of the OCI_Session class.
As a general rule, one must assume the process
handling the request is not connected to the database and that it
needs to. The $dbh handle contains username, password and the
destination database, so that is not a problem.
Furthermore, functions such as oci_parse,
oci_execute and oci_fetch_array are seen. These
functions are well known to anybody who has ever worked with Oracle.
The most basic function is oci_parse(), which parses a SQL statement and creates a “cursor”. Cursors are
Oracle handles for SQL statements.
The next function in the lot is oci_execute() which executes the parsed SQL statement. One function that
deserves mention (OCI8 will be explained in detail in the next
chapter) is oci_fetch_array(). It returns an array, which can
be addressed both as a hash using column names and as a regular array
using column numbers. This feature is extremely useful.
The missing function that is not seen in this
script is inserting. This function “binds”
PHP variable to a SQL variable. This makes it possible to use a PHP
variable to formulate a WHERE condition like the following:
SELECT ename,deptno, mgr,hiredate
FROM emp
WHERE empno = :EMPNO
The “bind” function makes it possible to
substitute a PHP variable instead of the SQL placeholder “:EMPNO”.
This also applies to the PL/SQL procedures and their arguments. The
purpose of bind is to avoid multiple parse calls as these are getting
increasingly expensive with the advent of the cost based optimizer.
Utilizing binds also makes HTML forms more secure and prevents common
tricks like SQL injection.
The script output is shown below:
This is the expected result of a query operating
on the two very well known Oracle demo tables. One last thing to
point out in this chapter is the use of HTML tables. The way HTML
table is used here is typical. It is convenient to display query
results as HTML table. All formatting is done by browser which knows
how to format HTML. Later in this book it will be demonstrated through
the use of PEAR even more convenient methods for displaying tables.
See
code depot for complete scripts
The above book excerpt is from:
Easy Oracle
PHP
Create Dynamic Web Pages with Oracle Data
ISBN
0-9761573-0-6
Mladen Gogala
http://www.rampant-books.com/book_2005_2_php_oracle.htm
|