This article mainly introduces the PHP connection to Oracle database and query data method, in case of the form of a more detailed analysis of PHP operation Oracle database using skills, with a certain reference value, the need for friends can refer to the next
This article explains how PHP connects to Oracle database and query data. Share to everyone for your reference. The specific analysis is as follows:
PHP has a powerful capability not only to support mysql,mssql,mysqli we can also connect with Oracle data, to make PHP support Oracle very simple we just put php.ini in; extention = Php_ The Oci8.dll semicolon is removed.
PHP supports Oracle connection functions
php.ini file configuration, remove; extention = Php_oci8.dll, remove the front semicolon, restart Apache, if not, we will put the PHP directory Php_ Oci8.dll to the Windows system under the System32.
The Oracle database establishes a link with the following code:
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 way not, using the second, the inside of a few parameters are user name, password, Oracle service address, where test is the service name, the code is as follows:
The code is as follows:
$sql = "SELECT * from Table_exmaple" $ora _test = Oci_parse ($conn, $sql); Compile SQL statement Oci_execute ($ora _test,oci_default); Execute while ($r =oci_fetch_row ($ora _test)) //Retrieve result {echo $ora _test[0]; echo "<BR>"; }
To see a complete example, if PHP version >5.0, then use the following function:
The code is as follows:
Oci_connect (username, password, dbname)
example, 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, prepare to execute if (! $stid) {$e = Oci_error ($conn); print htmlentities ($e [' message]); exit;} $r = Oci_execute ($stid, OC I_default); Execute SQL. Oci_default means do not automatically commit if (! $r) {$e = Oci_error ($stid); Echo htmlentities ($e [' message ']); exit;}//Print execution result print ' <ta ble border= "1" > "; while ($row = Oci_fetch_array ($stid, oci_return_nulls)) {print ' <tr> '; foreach ($row as $item) {print ' <td> '. ( $item? Htmlentities ($item): '). ' </td> '; } print ' </tr> '; } print ' </table> '; Oci_close ($conn);?>
The following is a pagination sample program, it may seem cumbersome, but in fact you just use it will find that everything is simple
example.php
<?php $conn = Ociplogon ("Test", "123456", "test123"); Include_once " Pager.inc.php ";";
<?php/** Page Test start *//{{{{} initial paging object $pager = new Pager ();/** select Id,name,age from Test where age>20 order by ID Desc Split */$sqlArr = array (' conn ' = = $conn,//Database link ' fields ' = ' id,name,age ',//query subject sentence ' table ' = ' test ',// Table name ' condition ' + ' age>20 ',//Query condition ' order ' = ' ORDER BY id desc '//sorting method; if (! $pager->listn ($SQLARR, ten, $page)//per page 10 {$pager->errno = ten; Die ($pager->errmsg ());}//}}}//{{{data display for ($i = 0; $i < count ($pager->result); $i + +) {$tmp = $pager->result[$i]; echo "ID:". $tmp [' id ']. " <br> "; echo "Name:". $tmp [' name ']. " <br> "; echo "Age:". $tmp [' age ']. "