Use PHP to operate Oracle Database 11. database connection in the previous section, we have introduced some basic operation knowledge about PHP and MySQL databases, and the most relevant PHP and MySQL tutorials on the Internet. MySQL is free, which may attract many people. Because of its wide application, I don't want to repeat MySQL usage here. Orac "> <LINKhref =" htt
Use PHP to operate Oracle databases
11. database connection
In the previous section, we have introduced some basic operation knowledge about PHP and MySQL databases, and the most relevant PHP and MySQL tutorials on the Internet. MySQL is free, which may attract many people. Because of its wide application, I don't want to repeat MySQL usage here. Oracle is widely used in enterprise applications, so we will use Oracle to further introduce the connection between PHP and database. Of course we will not mention the design principles of Oracle databases, because this is beyond our scope of discussion.
PHP provides two sets of functions connected to Oracle: ORA _ and OCI. The ORA _ function is somewhat outdated. OCI function updates are said to be better. The syntax is almost the same. As mentioned above, your PHP installation options should support both.
For more information about installing an Apache server that supports PHP3 on Microsoft Windows and about Oracle databases, see the following URL: www.csoft.net /~ Vsbabu/articles/oraphp.html.
11.1 connection
If ($ conn = Ora_Logon ("user @ TNSNAME", "password "))
{
Echo"SUCCESS! Connected to databaseN ";
}
Else
{
Echo"Failed:-(cocould not connect to databaseN ";
}
Ora_Logoff ($ conn );
Phpinfo ();
?>
The above code uses the Oracle database name, user name, and password defined by TNSNAME (specified in your tnsnames. ora file) to connect to the database. Based on successful connections, the ora_logon function returns a non-zero connection ID and stores it in the variable $ conn.
11.2 query
If the connection to the database is ready, we will query the database using the actual application. The following code demonstrates a typical connection and Query example:
/*
* Connect to the database and perform query
*/
Function printoraerr ($ in_cur)
{
// Check for Oracle errors
// If an error exists, it is displayed.
// This function is called after each request to Oracle when the pointer is activated
If (ora_errorcode ($ in_cur ))
Echo "Oracle code-". ora_error ($ in_cur). "n ";
Return;
}
/** Main program */
If (! ($ Conn = ora_logon ("user @ TNSNAME", "password ")))
{
Echo "Connection to database failedn ";
Exit;
}
Echo "Connected as connection-$ Conn
N ";
Echo "Opening cursor...
N ";
$ Cursor = ora_open ($ conn); printoraerr ($ cursor );
Echo "Opened cursor-$ Cursor
N ";
$ Qry = "select user, sysdate from dual ";
Echo "Parsing the query$ Qry...
N ";
Ora_parse ($ cursor, $ qry, 0); printoraerr ($ cursor );
Echo "Query parsed
N ";
Echo "Executing cursor...
N ";
Ora_exec ($ cursor); printoraerr ($ cursor );
Echo "Executed cursor
N ";
Echo "Fetching cursor...
N ";
While (ora_fetch ($ cursor ))
{
$ User = ora_getcolumn ($ cursor, 0); printoraerr ($ cursor );
$ Sysdate = ora_getcolumn ($ cursor, 1); printoraerr ($ cursor );
Echo "row =$ User, $ sysdate
N ";
}
Echo "Fetched all records
N ";
Echo "Closing cursor...
N ";
Ora_close ($ cursor );
Echo "Closed cursor
N ";
Echo "Logging off from oracle...
N ";
Ora_logoff ($ conn );
Echo "Logged off from oracle
N ";
?>
(Note: The above code segment lacks comments. For more information, see the Oracle database function section of PHP Manual)
11.3 Display results
The following code describes how to query a database and output the result:
Function printoraerr ($ in_cur, $ conn)
{
// Check for Oracle errors
// If an error exists, it is displayed.
// This function is called after each request to Oracle when the pointer is activated
// If it encountered an error, we exit immediately
If (ora_errorcode ($ in_cur ))
{
Echo "Oracle code-". ora_error ($ in_cur )."
N ";
Ora_logoff ($ conn );
Exit;
}
Return;
}
Function exequery ($ w_qry, $ conn)
{
$ Cursor = ora_open ($ conn); printoraerr ($ cursor, $ conn );
Ora_parse ($ cursor, $ w_qry, 0); printoraerr ($ cursor, $ conn );
Ora_exec ($ cursor); printoraerr ($ cursor, $ conn );
$ Numrows = 0;
$ W_numcols = ora_numcols ($ cursor );
// Display the header
Echo"
N ";
For ($ I = 0; $ I <$ w_numcols; $ I)
{
$ Align = (ora_columntype ($ cursor, $ I) = "NUMBER ")? "RIGHT": "LEFT ";
Echo "t
N ";
}
Echo"
N ";
While (ora_fetch ($ cursor ))
{
Echo"
N ";
For ($ I = 0; $ I <$ w_numcols; $ I)
{
$ Align = (ora_columntype ($ cursor, $ I) = "NUMBER ")? "RIGHT": "LEFT ";
If (ora_columntype ($ cursor, $ I) = "LONG ")
Echo"
N ";
Else
Echo"
N ";
Printoraerr ($ cursor, $ conn );
}
$ Numrows;
Echo"
N ";
}
If ($ numrows = 0)
Echo"
N ";
Else
{
Echo"
N ";
Echo"
N ";
Echo"
N ";
Echo"
N ";
}
Echo"
". Ora_columnname ($ cursor, $ I )." |
". ora_getcolumn($cursor,$i)." |
". Ora_getcolumn ($ cursor, $ I )." |
Query returned no records
|
Count |
$ Numrows |
N ";
Ora_close ($ cursor );
Return;
}// Main program
If (! ($ Conn = ora_logon ("user @ SID", "password ")))
{
Echo "Error: Cannot connect to databasen ";
Exit;
}
$ Qry = "SELECT
Deptno "Dept"
, Empno "Emp"
, Empnm "Name"
, Salary "Salary"
FROM
Employee
Order by 1, 2 ";
Exequery ($ qry );
Ora_logoff ($ conn );
?>
(Note: The above code segment lacks comments. For more information, see the Oracle database function section of PHP Manual)
11.4 HTTP-based Oracle login
Add the following code before the PHP page code to confirm Oracle login. Note that you must set $ SID correctly.
If (! Isset ($ PHP_AUTH_USER ))
{
Header ("WWW-authenticate: basic realm =" $ SID "");
Header ("HTTP/1.0 401 Unauthorized ");
$ Title = "Login Instructions ";
Echo"
You are not authorized to enter the site
N ";
Exit;
}
Else
{
If (! ($ Conn = ora_logon ("$ PHP_AUTH_USER @ $ SID", $ PHP_AUTH_PW )))
{
Header ("WWW-authenticate: basic realm =" $ SID "");
Header ("HTTP/1.0 401 Unauthorized ");
$ Title = "Login Instructions ";
Echo"
You are not authorised to enter the site
N ";
Exit;
}
}
?>