note 38612 deleted from ref.oracle by danbrown

From: Date: Fri, 28 Nov 2008 22:25:03 +0000
Subject: note 38612 deleted from ref.oracle by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-147328@lists.php.net to get a copy of this message
Note Submitter: http://iubito.free.fr ---- Here is my little but very very useful tutorial :-) CONNECTION : (in connect.php) <?php $ora_conn = ora_logon("databaseName@service","pass"); ?> ----------------------------- DISCONNECT : (in disconnect.php) <?php ora_logoff($ora_conn); ?> ----------------------------- In each page, you must include both files <?php include("connect.php"); blah blah include("disconnect.php"); ?> ----------------------------- Now the hard... SELECT ! (in select.php) <?php // Comment the line error_reporting if you want to // debug your code - for example the "NO DATA FOUND" // when no result. Oracle returns this like an error. error_reporting(0); // Create an array, a cursor, count number of cols, // perform the fetch and insert in the array. $results = array(); $ora_cur = ora_do($ora_conn, $query); // Don't panic, the $query will be set, wait a bit :) if ($ora_cur) { // Number of cols $numCols = ora_numcols($ora_cur); // Put the first line in the array... $row = array(); for($i=0; $i<$numCols; $i++) { // for each column $row[ora_columnname($ora_cur,$i)] = ora_getcolumn($ora_cur,$i); } array_push($results,$row); // "Fetch" all records, one by one, and create an array for each one. // Each array is "pushed" in the $results array. while (ora_fetch($ora_cur)) { // for each record $row = array(); for($i=0; $i<$numCols; $i++) { // for each column $row[ora_columnname($ora_cur,$i)] = ora_getcolumn($ora_cur,$i); } // Push $results array_push($results,$row); } } // Now turns error_reporting on. Comment this line if you want // to see "NO DATA FOUND" warnings. error_reporting(1); ?> -------------------------- And now let's enjoy with this simple page !! :) <? include("connect.php"); echo "blah blah"; $query = "select NAME, AGE from people where..."; include("select.php"); // Browse $results array and display results : if (count($results)) // if no "NO DATA FOUND" { reset($results); while($res=each($results)) { /* $res is like : array(0 => array('NAME' => 'Sylvain', 'AGE' => '21'), 1 => array('NAME' => 'Somebody','AGE' => '888')); */ echo $res[1]['NAME']; // name echo $res[1]['AGE']; // age // ALLWAYS write in uppercase ! } } else // NO_DATA_FOUND :( { echo "There's nobody in your database :D"; } echo "blah blah and again blah"; //...another queries $query = "..."; include("select.php");... include("disconnect.php"); ?> ---------------------- Another queries (insert, update...) are simpler. Let's see :) <?php include("connect.php"); $query = "UPDATE people SET url='http://iubito.free.fr' where name='Sylvain'"; include("query.php"); include("disconnect.php"); ?> and query.php is : <?php $cursor = ora_open($ora_conn); // Create a cursor if($cursor) // if created { // Parse query $resultat = ora_parse($cursor,$query); $resultat = ora_exec($cursor); // Execute $a=ora_commit($ora_conn); // perform COMMIT $b=ora_close($cursor); // close cursor } else { // Write an error message echo "oh, oh ! problem..."; } ?> Now enjoy using it ! For more information, see my website in French http://iubito.free.fr/prog/oracle.php

« previous php.notes (#147328) next »