Php database connection

Source: Internet
Author: User
Tags php database
Php database connection, read php database connection, 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 "> <LINKhref =" http: // ww, 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. 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 ";
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 );
?>
(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;
}
}
?>

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.