Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Translate DB connection and resultset info 1

Status
Not open for further replies.

dreampolice

Technical User
Dec 20, 2006
85
0
0
US
I am trying to figure out how this translates so I can use this piece of script I found to connect to an Oracle database and show record info:
Code:
include('config_basededatos_main.php'); //an include file that has database connection parameters?
$basededatos = connect_to_db ($Dbs["basededatos"]);  //not sure about this part 
$sqltecnicos="SELECT * FROM PRUEBAEXCEL"; 
$result = &$easymanage->Execute($sqltecnicos); //what is easymanage and how or what is this doing?
$count = $result->RecordCount(); 

for ($i = 0; $i < $count; $i++){ 
    $nombrefield=$result->FetchField($i); 
    $header .= $nombrefield->name.","; 
} 

while($row = $result->FetchRow()){ 
....
 
go back to first principles.

to use a database with php, typically you must
* connect to the db server
[* select the database] //for some db servers
* make the query
* iterate over the resultant recordset

sometimes "performing the query" is divided into two:
* prepare the query
* execute the query

for oracle - all the php functions can be seen in the relevant section of the manual:
a basic script would be:
Code:
<?php
$username = '';
$password = '';
$dbHost = '';  //this is not an ip address but an alias or instance name

$connection = oci_connect($username, $password, $dbHost);

//perform the query
$sql = "Select * from table";
$statement = oci_parse($sql); //prepare the query
oci_execute($statement); //execute the query

if (oci_num_rows($statement) > 0){	//test if there are any rows to output
	
	//iterate over the dataset
	echo "<table>\r\n";	
	while ($data = oci_fetch_assoc($statement)){
		
		//output the data
		foreach ($data as $fieldName=>$fieldValue){
			echo <<<HTML
	<tr>
		<td>{$fieldName}</td>
		<td>{$fieldValue}</td>
	</tr>
	
HTML;
		} // end foreach
		echo "<tr><td>&nbsp;</td><td>&nbsp;</td></tr>\r\n"; //insert blank row to separate records
	} //end while
} else {
	echo "No Records to Output";
}//end if
?>
 
Thanks for taking the time to give me a great example.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top