The 12th section uses PHP to operate the Oracle database _php
Source: Internet
Author: User
Keywords database operation use 12 Cursorechoconn
using PHP to manipulate Oracle databases
One . database connections
in the previous section, we have introduced php and mysql Some basic operational knowledge of the database in the internet about php and mysql Tutorials are also the most. mysql is free, which may attract a lot of people. Because of its wide application, I do not want to repeat here mysql method of use. oracle is widely used in enterprise applications, so we will use oracle to further introduce PHP connection to the database. We will certainly not mention the design principle of oracle database, because this is beyond our scope of discussion.
PHP provides two sets of functions to connect with Oracle , namely the Ora_ and OCI functions. Where the Ora_ function is slightly stale. The OCI function Update is said to be better. The use of the two grammars is almost the same. As mentioned earlier, your PHP installation options should be able to support the use of both. For more information on installing a PHP3 -enabled Apache server on the Microsoft Windows platform and more on Oracle databases, Please refer to the following URL:www.csoft.net/~vsbabu/articles/oraphp.html.
11.1 Connection
!--? if ($ conn = ora_logon (" user@tnsname", "Password ")) { echo " SUCCESS! Connected to Database n "; } Else { echo Failed:-( Could not connect to database N ";
ora_logoff ($conn ); phpinfo (); ? The above code connects to the database using the Oracle database name, user name, and password defined by Tnsname(indicated in your tnsnames.ora file). On the basis of a successful connection, theora_logon function returns a nonzero connection ID and stores it in the variable $conn .
11.2 Enquiry
Assuming the connection with the database is ready, let's take a look at the actual application of the database query. The following code shows a typical example of a connection and query: /* * Connect to the database and execute the query */ function printoraerr($in_cur) { // CheckOracleis error // Display if there is an error // When the pointer is activated, each requestOracleCall this function after the 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 DatabaseFailedn"; Exit } echo "Connected as connection-$Conn <BR>n "; echo "Opening cursor ...<BR>n "; $cursor =Ora_open($Conn);Printoraerr($cursor); echo "opened cursor-$cursor<BR>n "; $qry= "Selectuser,sysdatefrom dual "; echo "Parsing the query$qry ...<BR>n "; Ora_parse($cursor, $qry, 0);Printoraerr($cursor); echo "Query parsed <BR>n "; echo "Executing cursor ...<BR>n "; ora_exec($cursor);Printoraerr($cursor); echo "Executed cursor<BR>n "; echo "Fetching cursor ...<BR>n "; WhileOra_fetch($cursor)) { $user =Ora_getcolumn($cursor, 0);Printoraerr($cursor); $sysdate=Ora_getcolumn($cursor, 1);Printoraerr($cursor); echo "row =$user, $sysdate <BR>n "; } echo "fetched all records<BR>n "; echo "Closing cursor ...<BR>n "; Ora_close($cursor); echo "Closed cursor<BR>n "; echo "Logging off from Oracle ... <BR>n "; Ora_logoff($Conn); echo "Logged off from Oracle <BR>n "; ?> (Translator Note: The above code snippet is missing comments, please refer to the readerPHP ManualOfOracleDatabase functions section)
11.3 Show results
!--? function printoraerr ($ in_cur , $ conn ) { // check oracle error // display If there is an error // when the pointer is activated each time a request oracle is called after the function /If it Encountered an error, we exit immediately if (ora_errorcode ($in_ Cur ) { echo "Oracle code-". Ora_error ($in_cur ). " <br >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); Show Head echo "
n "; For ($i=0; $i <$w_numcols; $i+ +) { $align = (ora_columntype($cursor, $i) = = "Number")? " Right ":" Left "; echo "Tn "; } 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 "n "; Else echo "n "; Printoraerr ($cursor, $conn); } $numrows+ +; echo "n "; } if ($numrows==0) echo "n "; Else { echo "n "; echo "n "; echo "n "; echo "n "; } echo "
". Ora_columnname ($cursor, $i). "
". Ora_getcolumn ($cursor, $i). "
". Ora_getcolumn ($cursor, $i). "
Query returned no records
Count
$numrows
n "; Ora_close($cursor); Return }
// Main Program if (! ( $Conn=Ora_logon("user@sid", "Password"))) { echo "Error:cannot connect to databasen"; Exit } $qry= "Select Deptno "Dept" ,empno "Emp" ,empnm "Name" , salary "salary" From Employee ORDER by "; Exequery ($qry); Ora_logoff ($conn); ?> (Translator Note: The above code snippet is missing comments, please refer to the Oracle database Functions section of PHP Manual )
11.4 http oracle login
add the following code to the php page code to confirm the oracle login. Note that you must correctly set the $ SID . !--? if (! ($PHP _auth_user)) { Header ("Www-authenticate:basic realm=" $SID ""); Header ("http/1.0 401 Unauthorized"); $title = "Login instructions"; echo "<blockquote ; You is not authorized to enter the site !--blockquote > 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 "<blockquote ; You is not authorised to Enter the site !--blockquote > n "; Exit } } ?>
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.