Connect to the database. 4. Data Library Connection with PHP you can easily connect to the database, request data and display it on your web site, or even modify the data in the database. MySQL is a popular database 4. database connection
With PHP, you can easily connect to the database, request data and display it on your web site, or even modify data in the database. MySQL is a very popular database, and there are many tutorials on PHP and MySQL 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 use Oracle to 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.
4.1 Connection
If ($ conn = Ora_Logon ("user @ TNSNAME", "password "))
{
Echo"
SUCCESS! Connected to database\ N ";
}
Else
{
Echo"Failed:-(cocould not connect to database\ N ";
}
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.
4.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 failed \ n ";
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)
4.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
". Ora_columnname ($ cursor, $ I )." | \ 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"
". ora_getcolumn($cursor,$i)." | \ N ";ElseEcho"
". Ora_getcolumn ($ cursor, $ I )." | \ N ";Printoraerr ($ cursor, $ conn );}$ Numrows ++;Echo"
\ N ";}If ($ numrows = 0)Echo"
Query returned no records
|
\ N ";Else{Echo"
\ N ";Echo"
Count | \ N ";Echo"
$ Numrows | \ N ";Echo"
\ N ";}Echo"
\ N ";
Ora_close ($ cursor );
Return;
}
// Main program
If (! ($ Conn = ora_logon ("user @ SID", "password ")))
{
Echo "Error: Cannot connect to database \ n ";
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)
4.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;
}
}
?>
Using PHP, you can easily connect to the database, request data, display it on your web site, or even modify data in the database. MySQL is a very popular database...