This article mainly introduces how to connect php to the oracle database and query data. it analyzes in detail how to use php to operate the oracle database in the form of an instance, which has some reference value, for more information about how to connect php to the oracle database and query data, see the following example. Share it with you for your reference. The specific analysis is as follows:
Php has powerful functions that not only support mysql, mssql, and mysqli, we can also connect to oracle Data. to make php support for oracle very simple, we just need to put php. in ini, remove the; extention = php_oci8.dll semicolon.
Php supports oracle connection functions
Php. in the INI file, remove the configuration; extention = php_oci8.dll, remove the semicolon, and restart apache. if not, copy php_oci8.dll in the php directory to system32 in windows.
The code for creating a connection to the oracle database is as follows:
1.
The code is as follows:
$ Conn = oci_connect ('username', 'password', "(DEscriptION = (ADDRESS = (PROTOCOL = TCP) (HOST = 192.168.1.100) (PORT = 1521 )) (CONNECT_DATA = (SID = test )))");
2.
The code is as follows:
$ Conn = oci_connect ('username', 'password', '192. 168.1.100/test ');
3. Oracle connection method:
The code is as follows:
Set adocon = Server. Createobject ("adodb. connection ")
Adocon. open "Driver = {microsoft odbc for oracle}; server = oraclesever. world; uid = admin; pwd = pass ;"
4. Oracle ole db connection method:
The code is as follows:
Set adocon = Server. Createobject ("adodb. connection ")
Adocon. open "Provider = OraOLEDB. Oracle; data source = dbname; user id = admin; password = pass ;"
Sometimes the first method does not work. The second method is used. the parameters are the user name, password, and oracle service address. test is the service name and the code is as follows:
The code is as follows:
$ SQL = "select * from table_exmaple"
$ Ora_test = oci_parse ($ conn, $ SQL); // compile an SQL statement
Oci_execute ($ ora_test, OCI_DEFAULT); // execute
While ($ r = oci_fetch_row ($ ora_test) // Retrieve the result
{
Echo $ ora_test [0];
Echo"
";
}
Take a complete example. If PHP version is later than 5.0, use the following function:
The code is as follows:
Oci_connect (username, password, dbname)
The code is as follows:
The code is as follows:
<? Php
$ Conn = oci_connect ('HR', 'HR', 'orcl '); // establish a connection
If (! $ Conn ){
$ E = oci_error ();
Print htmlentities ($ e ['message']);
Exit;
}
$ Query = 'select * FROM departments'; // query statement
$ Stid = oci_parse ($ conn, $ query); // Configure the SQL statement and prepare for execution.
If (! $ Stid ){
$ E = oci_error ($ conn );
Print htmlentities ($ e ['message']);
Exit;
}
$ R = oci_execute ($ stid, OCI_DEFAULT); // run the SQL statement. OCI_DEFAULT indicates that automatic commit is not allowed.
If (! $ R ){
$ E = oci_error ($ stid );
Echo htmlentities ($ e ['message']);
Exit;
}
// Print the execution result
Print'
';While ($ row = oci_fetch_array ($ stid, OCI_RETURN_NULLS )){Print'
';Foreach ($ row as $ item ){Print'
'. ($ Item? Htmlentities ($ item ):'').' | ';}Print'
';}Print'
';
Oci_close ($ conn );
?>
I hope this article will help you with php programming.