Connection between PHP and database

Source: Internet
Author: User
4. Data Library Connection with PHP you can easily connect to the database, beg for 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. 4. database connection
With PHP, you can easily connect to the database, request data and display it on your web site, or even correct the data in the database. MySQL is a very popular database, and there are many PHP and MySQL tutorials on the Internet. MySQL is free, which may attract many people. Since it is widely used, I don't want to repeat the MySQL application method here. Oracle is widely used by enterprises, so we use Oracle to connect PHP to databases first. 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, which can be distinguished by ORA _ and OCI functions. The ORA _ function is somewhat stale. OCI function updates are said to be better. The application syntax is almost the same. As mentioned above, your PHP installation options should support both applications.
For more information about installing the Apache server supporting 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 applies 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 has been stopped, we will use the actual query to the database. The following code demonstrates a typical example of connection and query:
/*
* 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 every time Oracle is requested 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 'connectedas 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 ';

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.