PHP ODBC Functions

By | August 29, 2020
PHP ODBC Functions

PHP supports the Open database connectivity which is an API that enables developers to abstract a program from the database.

PHP ODBC Functions

autocommit

This function will provide you with toggle autocommit behaviour.

Vamware

Syntax

mixed odbc_autocommit ( resource $connection_id [, bool $OnOff = false ] )

Example

<?php
   $input = odbc_connect("DSN", "user", "pass");
   $Return = odbc_autocommit($input, FALSE);
?>

binmode

This function will allow you to handle the binary column data.

Syntax

bool odbc_binmode ( resource $result_id , int $mode )

Close all

This function will allow you to close all odbc connections and this function will not take any parameters and does not return any value.

Syntax

void odbc_close_all ( void )

Example

<?php
   odbc_closeall();
?>

Close

This function will allow you to close an odbc connection

Syntax

void odbc_close ( resource $connection_id )

Example

<?php
   $input = odbc_open("XXX","user","pass");
   odbc_close($input);
?>

Columnprivileges

This function will provide you with information about the list of the column and the details of the privileges that are associated with the specified table.

Syntax

resource odbc_columnprivileges ( resource $connection_id , string $qualifier , string $owner , string $table_name , string $column_name )

Columns

This function will provide you with the list of column names that are specified within the given table.

Syntax

resource odbc_columns ( resource $connection_id [, string $qualifier  [, string $schema [, string $table_name [, string $column_name ]]]] )

Example

<?php
   include('header.php'); 
   $val = odbc_columns($conn, "Db_name", "%", "table_name", "%");
   $pages = array();   
   while (odbc_fetch_into($val, $pages)) {
      echo $pages[3] . "<br />\n";
   }
?>

Commit

This function will commit the odbc transaction for the specified connection id.

Syntax

bool odbc_commit ( resource $connection_id )

Connect

This function will connect you with the ODBC data source

Syntax

resource odbc_connect ( string $dsn , string $user , string $password [, int $cursor_type ] )

Cursor

This function will provide you with the cursor and is returned as the string.

Syntax

string odbc_cursor ( resource $result_id )

Example

<?php
   $ID = odbc_connect("DSN","user_id","pass_id");
   $res = odbc_exec($ID,"SELECT FirstName, LastName FROM Students ORDER BY LastName");   
   echo odbc_cursor($res);
?>

Data Source

This function will provide the information about the current connection which is specified as the connection id along with the fetch type which defines the fetch id.

Syntax

array odbc_data_source ( resource $connection_id , int $fetch_type )

Error

This function will provide you with information about the last error based on the provided connection id.

Syntax

string odbc_error ([ resource $connection_id ] )

Example

<?php
   if (odbc_error()){
      echo odbc_errormsg($conn);
   }      
?>

Errormsg

This function will allow you to provide the last error message based on the provided connection id.

Syntax

string odbc_errormsg ([ resource $connection_id ] )

Example

<?php
   {
      $ID = odbc_connect("DSN","user_id","pass_id");
      $db_sql = "SELECT ProductName, UnitPrice FROM Data";
      if (!$result = @odbc_exec($ID, $db_sql)) {
         echo "Query error! ODBC error: ", odbc_errormsg();
      }}
      ?>

Exec

This function will allow you to prepare and execute those Sql statements

Syntax

bool odbc_execute ( resource $result_id [, array $parameters_array ] )

fetch array

This function will allow getting the result row in the form of the associated array

Syntax

array odbc_fetch_array ( resource $result [, int $rownumber ] )

fetch into

This function will allow the result to be fetched into the row.

Syntax

int odbc_fetch_into ( resource $result_id , array &$result_array [, int $rownumber ] )

Fetch_object

This function will allow the result to be fetched in the row as an object

Syntax

object odbc_fetch_object ( resource $result [, int $rownumber ] )

Example

<?php
   $ID = odbc_connect($db_name, $username, $password) or die(odbc_error_msg());
   $db_sql = "SELECT * FROM TABLE";
   $result = odbc_exec($ID, $db_sql);
   while ($rows = odbc_fetch_object($result)) {
      print $rows->COLUMNNAME;
   }   
   odbc_close($ID); 
?>

fetch row

This function will allow you to fetch the row

Syntax

bool odbc_fetch_row ( resource $result_id [, int $row_number ] )

Example

<?php
   $ID = odbc_connect("DSN","user_id","pass_id");
   $db_sql = "SELECT ProductName FROM Products";
   $res = odbc_exec($ID, $db_sql);
   while (odbc_fetch_row($res)) {
      echo odbc_result($res, "ProductName"), "\n";
   }
?>

field len

This function will provide you with the length of a filed

Syntax

bool odbc_fetch_row ( resource $result_id [, int $row_number ] )

Example

<?php
   $ID = odbc_connect("DSN","user_id","pass_id");
   $db_sql = "SELECT * FROM Students";
   $res = odbc_exec($ID, $db_sql);
   odbc_fetch_row($res);
   for ($col = 1; $col<=odbc_num_fields($res); $col++) {
      printf("Column %s has length %s\n", odbc_field_name($res, $col), odbc_field_len($res, $col));
   }
?>

filed name

This function will provide you with the column name

Syntax

string odbc_field_name ( resource $result_id , int $field_number )

field num

This function will provide you with the filed number

Syntax

int odbc_field_num ( resource $result_id , string $field_name )

field scale

This function will provide you with the scale of the field.

Syntax

int odbc_field_scale ( resource $result_id , int $field_number )

Example

<?php
   $ID = odbc_connect("DSN","user_id","pass_id");
   $db_sql = "SELECT * FROM Students";
   $res = odbc_exec($ID, $db_sql);
   odbc_fetch_row($res);
   for ($col = 1; $col<=odbc_num_fields($res); $col++) {
      printf("Column %s has length %s\n", odbc_field_name($res, $col), odbc_field_scale($res, $col));
   }
?>

field type

This function will provide you with the datatype information of a filed

Syntax

string odbc_field_type ( resource $result_id , int $field_number )

Foreignkeys

This function will allow you to provide a list of foreign keys.

Syntax

resource odbc_foreignkeys ( resource $connection_id , string $pk_qualifier ,  string $pk_owner , string $pk_table , string $fk_qualifier , string $fk_owner , string $fk_table )

Example

<?php
   $ID = odbc_connect("DSN","user_id","pass_id");
   $res = odbc_foreignkeys($ID,"XXX", "dbo", "Subjects", "Data", "dbo1", "Students"
);
   odbc_result_all($res);
?>

free result

This function will provide you with the free resource that is specified with the help of result id.

Syntax

bool odbc_free_result ( resource $result_id )

Example

<?php
   $ID = odbc_connect("DSN","user_id","pass_id");
   $res = odbc_exec($ID,"SELECT * FROM Students ORDER BY StudentName");
   odbc_free_result($res);
?>

Gettypeinfo

It is used to retrieve the information about data types

Syntax

resource odbc_gettypeinfo ( resource $connection_id [, int $data_type ] )

Longreadlen

This function will allow you to handle the long columns

Syntax

bool odbc_longreadlen ( resource $result_id , int $length )

Example

<?php
   $ID = odbc_connect("DSN","user_id","pass_id");
   $res = odbc_exec($ID, "SELECT Notes FROM Students");
   odbc_longreadlen($res, 8080);
   echo odbc_result($res,1);
?>

next result

This function will allow you to check if multiple results are available for the provided result id.

Syntax

bool odbc_next_result ( resource $result_id )

num rows

This function will provide you with the number of rows present in a result id.

Syntax

int odbc_num_rows ( resource $result_id )

pconnect

This function will allow you to open a persistent database connection

Syntax

resource odbc_pconnect ( string $dsn , string $user , string $password [, int $cursor_type ] )

Example

<?php
   $db = "DSN";
   if (!$db = odbc_pconnect($db,"user_id","pass_id")) {
      echo "could not connect to $db!\n";
      exit;
   }
?>

prepare

This function will allow you to prepare a statement for execution

Syntax

resource odbc_prepare ( resource $connection_id , string $query_string )

primarykeys

This function will allow you to provide the primary key for a provided table for a given connection id.

Syntax

resource odbc_primarykeys ( resource $connection_id , string $qualifier , string $owner , string $table )

procedurecolumns

This function will allow you to provide the information about the parameters of procedures for a particular connection id.

Syntax

resource odbc_procedurecolumns ( resource $connection_id )

Example

<?php
   $db = odbc_connect("DSN","user_id","pass_id");
   $res = odbc_procedurecolumns($db,"Data", "dbo1", "Students Status By Subjects","%");
   odbc_result_all($res);
?>

procedures

This function will allow you to provide the procedures stored in a specific data resources

Syntax

resource odbc_procedures ( resource $connection_id )

result all

This function will allow you to display the result in a HTML table

Syntax

int odbc_result_all ( resource $result_id [, string $format ] )

Example

<?php
   $db = odbc_connect("DSN","user_id","pass_id");
   $res = odbc_procedurecolumns($db,"Data", "dbo1", "Students Status By Subjects","%");
   odbc_result_all($res);
?>

Result

This function will provide you with the current result as an HTML table

Syntax

mixed odbc_result ( resource $result_id , mixed $field )

Rollback

This function will allow you to roll back the transaction

Syntax

bool odbc_rollback ( resource $connection_id )

Setoption

This function is used to adjust the odbc settings for the provided id and the details to make changes.

Syntax

bool odbc_setoption ( resource $id , int $function , int $option , int $param )

Specialcolumns

This function will provide you with the details of the special columns.

Syntax

resource odbc_specialcolumns ( resource $connection_id , int $type ,  string $qualifier , string $owner , string $table , int $scope , int $nullable )

Example

<?php
   $db = odbc_connect("DSN","user_id","pass_id");
   $res = odbc_specialcolumns($db,"Data", "dbo1", "Students Status By Subjects","%");
   odbc_result_all($res);
?>

Statistics

This function will provide you with the statics about the table for the provided connection id.

Syntax

resource odbc_statistics ( resource $connection_id , string $qualifier , string $owner , string $table_name , int $unique , int $accuracy )

Tableprivileges

This function will provide the list of the tables and privileges associated with each table for the provided connection id.

Syntax

resource odbc_tableprivileges ( resource $connection_id ,  string $qualifier , string $owner , string $name )

Example

<?php
   $db = odbc_connect("DSN","user_id","pass_id");
   $res = odbc_tableprivilidges($db,"Data", "dbo1", "Students Status By Subjects");   
   odbc_result_all($res);
?>

Tables

This function will provide you with the list of tables which are stored in data sources

Syntax

resource odbc_tables ( resource $connection_id [, string $qualifier [, string $owner [, string $name [, string $types ]]]] )

Example

<?php
   $db = odbc_connect($dsn, $user_id, $pwd_id);
   $res = odbc_tables($db);
   $tables = array();   
   while (odbc_fetch_row($res)){
      if(odbc_result($res,"TABLE_TYPE")=="TABLE")
      echo" ".odbc_result($res,"TABLE_NAME");
   }
?>

Leave a Reply

Your email address will not be published. Required fields are marked *