|
Here are some example scripts to connect to databases in php, using
If you work on the Windows server then you can use COM-object for connection to database. Here are examples for Microsoft Access database and Oracle database :
<?php $connection=New COM("ADODB.Connection"); $rs = New COM("ADODB.Recordset"); # Connection strings $oracle='DRIVER={Microsoft ODBC for Oracle};SERVER=devdb;UID=username;PWD=password'; $msaccess = 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\path\to\msaccess\database.mdb;'; $databaseOfMyChoice = $msaccess; // or $oracle $connection->ConnectionString = $databaseOfMyChoice; $connection->Open(); # Sql table $table = "my_sql_table"; $sql = "SELECT * FROM $table"; $rs->Open($sql, $connection); # Assosiative array $assoc_array = array(); # Number $index = 0; # The below code will create an array (depending on the fields of the database table of course): # Click to see the array created by the code below /* Array( [0] => Array ( [ID] => 1 [NAME] => name1 ) [1] => Array ( [ID] => 2 [NAME] => name2 ) ) */ # Iterate through our result while (!$rs->EOF) { for( $x = 0; $x < $rs->Fields->Count; $x++ ) { $assoc_array[ $index ][ $rs->Fields[$x]->Name ] = $rs->Fields[$x]->Value; } # Move cursor to next row in recordset $rs->MoveNext(); $index++; } # Print the array echo "<pre>"; print_r($assoc_array); echo "</pre>"; # Close the connection and the recordset $rs->Close(); $connection->Close(); ?>
For Unix and Windows servers you can connect to an Oracle database using these examples:
# Connection to an Oracle database using oci8 functions on WINDOWS OS or LINUX, PHP version 4.3.6 # There will be some changes from the currently installed version 4.3.6 to version 5.0.0. At the # Present moment version 5.0.0 is not considered stable, so until it is apply the following note when using oci # to connect to oracle databases. # Note: In PHP versions before 5.0.0 you must use ociexecute() instead of oci_execute(). # This is the case for most of the oci functions, i.e. ocilogon, ociparse... # For further reference se bottom of page $db = "devdb"; # or $db = "(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = database_host)(PORT = 10521))(CONNECT_DATA = (SID = D)))"; $username = "your username"; $password = "your password"; # open a connection $conn = ocilogon($username, $password, $db); # A sql table $table = "my_sql_table"; # Prepare the sql statement $stmt = ociparse($conn,"select * from $table"); # Execute the statement ociexecute($stmt, OCI_DEFAULT); # Fetch result while ( OCIFetchInto ($stmt, $row, OCI_ASSOC) ) { # See also ocifetch echo "<pre>"; print_r($row); echo "</pre>"; }
For Windows servers you can connect to an Oracle or Microsoft Access using ODBC database through these examples:
# Connecting to an Oracle or Microsoft Access database using ODBC on WINDOWS OS # See www.php.net for further reference $ora_dsn= "DRIVER={Microsoft ODBC for Oracle};SERVER=servername;UID=username;PWD=password"; $msa_dsn = "DRIVER=Microsoft Access Driver (*.mdb); DBQ=c:\path\to\msaccess\database.mdb;"; $dsn = $ora_dsn; # or $msa_dsn $username = ""; $password = ""; # open a connection $conn = odbc_connect($dsn, $username, $password); # The sql table $table= "my_sql_table"; #Prepare the sql statement $stmt= odbc_prepare($conn,"select * from $table"); # Execute the statement odbc_execute($stmt); #Fetch result. echo "Executing sql statement: select * from $table\n"; echo "Printing the php assosiative array returned by odbc_fetch_array\n"; while ( $row = odbc_fetch_array ($stmt)) { echo "<pre>"; print_r($row); echo "</pre>"; } # Once more $stmt = odbc_prepare($conn,"select * from $table"); odbc_execute($stmt); # Fetch result. echo "Executing sql statement: select * from $table\n"; echo "Printing one field from the assosiative array returned by odbc_fetch_array\n"; while ( $row = odbc_fetch_array ($stmt) ) { echo "<pre>"; echo "familyname = " . $row['FAMILYNAME']; echo "</pre>"; }