Home > Backend Development > PHP Tutorial > How to connect to oracle database and query data in php, oracle database_PHP tutorial

How to connect to oracle database and query data in php, oracle database_PHP tutorial

WBOY
Release: 2016-07-13 10:10:35
Original
897 people have browsed it

How to connect oracle database with php and query data, oracle database

The example in this article describes how to connect PHP to the Oracle database and query data. Share it with everyone for your reference. The specific analysis is as follows:

php has powerful functions. Not only can it support mysql, mssql, mysqli, we can also connect with oracle data. It is very simple to make php support oracle. We only need to put the semicolon in php.ini; extension = php_oci8.dll Just remove it.

php supports oracle connection function

Remove the configuration in the php.ini file; extension = php_oci8.dll, remove the semicolon in front, and restart apache. If it doesn’t work, we will copy the php_oci8.dll in the php directory to system32 of the windows system. Go down below.

Establish a link to the oracle database, the code is as follows:

1.

Copy code 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.
Copy code The code is as follows:
$conn = oci_connect('username','password','192.168.1.100/test');

3.Oracle connection method:
Copy code 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:
Copy code 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, so use the second method. The parameters are user name, password, oracle service address, where test is the service name, and the code is as follows:
Copy code 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 the results
{
echo $ora_test[0];
echo "
";
}

See a complete example, if PHP version >5.0, then use the following function:
Copy code The code is as follows:
oci_connect ( username, password, dbname )

Example, the code is as follows:
Copy code The code is as follows:
$conn = oci_connect('hr', 'hr', 'orcl'); // Establish connection
if (!$conn) {
$e = oci_error();
print htmlentities($e['message']);
exit;
}
$query = 'SELECT * FROM DEPARTMENTS'; // Query statement
$stid = oci_parse($conn, $query); // Configure SQL statements and prepare for execution
if (!$stid) {
$e = oci_error($conn);
print htmlentities($e['message']);
exit;
}
$r = oci_execute($stid, OCI_DEFAULT); // Execute SQL. OCI_DEFAULT means do not commit automatically
if(!$r) {
$e = oci_error($stid);
echo htmlentities($e['message']);
exit;
}
//Print execution results
print '';
while($row = oci_fetch_array($stid, OCI_RETURN_NULLS)) {
print '';
foreach($row as $item) {
print '';
}
print '';
}
print '
'.($item?htmlentities($item):' ').'
';
oci_close($conn);
?>

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/934925.htmlTechArticleHow to connect php to oracle database and query data, oracle database This article describes how to connect php to oracle database and query data method. Share it with everyone for your reference. Detailed analysis...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template