Connect to Oracle database with PHP _ PHP Tutorial

Source: Internet
Author: User
Use PHP to connect to the Oracle database. 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 you can easily connect to the database through PHP, request data and display it on your web site, or even modify the 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. Your PHP installation options should support both.

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.

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:

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;
}
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 ";
?>

Display result

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 ";
Else
Echo "". 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 );
?>


HTTP-based Oracle logon

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;
}}
?>

Bytes. MySQL is a very popular database, and in mutual...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.