ODBC

One of PHP's strengths is database connectivity. PHP supports a large number of databases and simplifies access via a unified ODBC interface. This provides a number of useful functions. I hava also written a number of helper functions for form building using DB2 on CS1. To use them put require(db2func.pinc) in your file. See db2func.pinc or db2func.phps (prettyprint source)

Connecting and retrieving results from a database using the ODBC interface requires the following steps:

  1. Connect:
           /* need to set $dsn: data source name
                          $user: what user to connect as
    		      $upasswd: password for the user
    	  in our examples we simply set the $dsn. The user is
    	         the user the web server runs as
    		 no password is required.
    		 This is a DB2 thing  */
    		 
            $conn  =  odbc_connect($dsn,  $user,$upasswd  );
            echo  "conn:  $conn";
            if  ($conn  <=  0)  {
                    echo  "Error  in  connection<BR>";
                    exit;
                      }
            else  {
                    echo  "<P>Connection  successful\n";
                      };
  2. Build and submit a query:
         $query  =  "SELECT  *  FROM  DBMS.$Table";
         $result  =  odbc_Exec($conn,  $query)
  3. Display results. Here you can use odbc_result_all($result); to display the entire result as a table or use a loop with odbc_fetch_row($result) to select each row and odbc_result($result, "attribute") to fetch individual attributes in the row
  4. Close connection: odbc_Close($conn);

PHP | Examples

Search |  Computer Science |  Feedback
Site Map