Connect to the Oracle database using PHP

Source: Internet
Author: User
Tags php and php and mysql
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. Your PHP installation options should support both.

Connection
<? If ($ conn = Ora_Logon ("user @ TNSNAME", "password "))
{Echo "SUCCESS! Connected to database ";
} Else
{Echo "Failed:-(cocould not connect to database ";}
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 )."";
Return;
}
If (! ($ Conn = ora_logon ("user @ TNSNAME", "password ")))
{Echo "Connection to database failed ";
Exit;
}
Echo "Connected as connection-$ conn ";
Echo "Opening cursor ...";
$ Cursor = ora_open ($ conn); printoraerr ($ cursor );
Echo "Opened cursor-$ cursor ";
$ Qry = "select user, sysdate from dual ";
Echo "Parsing the query $ qry ...";
Ora_parse ($ cursor, $ qry, 0); printoraerr ($ cursor );
Echo "Query parsed ";
Echo "Executing cursor ...";
Ora_exec ($ cursor); printoraerr ($ cursor );
Echo "Executed cursor ";
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.