PHPBuilder - Interfacing with MS Access Page 5



RSS Twitter
Articles Databases

Interfacing with MS Access - Page 5

by: Siddharth Venkatesan
|
July 30, 2000

The code on the prior page is pretty common place. Now we actually interface with the database. We can do this using the inherent ODBC functions within PHP and SQL. As the above form says, the action is going to be DataAccess.php3. So copy and paste the code below into the file DataAccess.php3.

<?php

function HTML_Head() {
    echo 
"
    <HTML><HEAD>
    <TITLE>Processing Form</TITLE>
    </HEAD>
    <BODY BGCOLOR=\"#D5D5AB\">"
;
}

function 
HTML_Foot() {
    echo 
"</body></html>";
}

function 
Database_Entries($msg) {
    echo 
$msg;
}

function 
Output_Entries() {
    
/*
        Make the connection to the database. The syntax is 
        odbc_connect( 'SYSTEM_DSN' , 'USER', 'PASSWORD' );
        $cnx will hold the
        pconnect is used to establish a persistent database 
        connection to the Database until the procedure is completed.
    */

    
$cnx odbc_connect'WebTute' 'root''' );
    if (!
$cnx) {
        
Error_handler"Error in odbc_connect" $cnx );
    }

    
// send a simple odbc query . returns an odbc cursor
    
$curodbc_exec$cnx"select Index,FirstName,LastName,PhoneNumber from People" );
    if (!
$cur) {
        
Error_handler"Error in odbc_exec( no cursor returned ) " $cnx );
    }
    echo 
"<table border=1><tr><th>Index</th><th>First Name</th>".
        
"<th>Last Name</th><th>Phone Number</th></tr>\n";
    
$nbrow=0;   //Local variable to count number of rows

    // fetch the succesive result rows
    
while( odbc_fetch_row$cur ) ) {
        
$nbrow++;
        
$Indexodbc_result$cur); // get the field "Index"
        
$FirstNameodbc_result$cur); // get the field "FirstName"
        
$LastNameodbc_result$cur); // get the field "LastName"
        
$PhoneNumberodbc_result$cur); // get the field "PhoneNumber"

        
echo "<tr><td>$Index</td><td>$FirstName</td>".
            
"<td>$LastName</td><td>$PhoneNumber</td></tr>\n";
    }

    echo 
"<tr><td colspan=2>$nbrow entries </td></tr></table>";

    
// close the connection. important if persistent connection are "On"
    
odbc_close$cnx);
}

function 
Error_Handler$msg$cnx ) {
    echo 
"$msg \n";
    
odbc_close$cnx);
    exit();
}

function 
Enter_New_Entry($FirstName,$LastName,$PhoneNumber) {

    
/*
        First, we create a connection to our ODBC source. This is done by creating
        a connection. Once this is done, we are returned an ODBC connection number.
        We use this number to use the ODBC functions within PHP.
    */

    
$cnx odbc_connect'WebTute' 'root''' );
    if (!
$cnx) {
        
Error_handler"Error in odbc_connect" $cnx );
    }
    
$SQL_Exec_String "Insert Into People (FirstName, LastName, PhoneNumber)
            Values ('$FirstName', '$LastName', '$PhoneNumber')"
;

    
$curodbc_exec$cnx$SQL_Exec_String );
    if (!
$cur) {
        
Error_handler"Error in odbc_exec( no cursor returned ) " $cnx );
    }

    
odbc_close$cnx);
}

$strOldEntries "Previous Entries in database";
$strNewEntries "Updated version of databse (after entries)";

HTML_Head();
Database_Entries($strOldEntries);
Output_Entries();
Enter_New_Entry($FirstName,$LastName,$PhoneNumber);
Database_Entries($strNewEntries);
Output_Entries();
HTML_Foot();

?>
Now you should be ready to go! In this example, i havent done any deletion of records. However, as you can see, if you modify the variable SQL_Exec_String, you can delete specific records as well.
I hope this has been of use to you.
--Sid

« Previous Page
1
|
2
|
3
|
4
|
5

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Siddharth Venkatesan

Comment:



Comment:

(Maximum characters: 1200). You have characters left.