Set odbc connection using php

Using ODBC with PHP ODBC is one of Microsoft’s earliest technologies for connecting to databases. It is very popular and widely used. This article describes how to setup and use ODBC from a PHP perspective.

Here are the steps for odbc connection:

<?php
// Microsoft SQL Server using the SQL Native Client 10.0 ODBC Driver - allows connection to SQL 7, 2000, 2005 and 2008
$connection = odbc_connect("Driver={SQL Server Native Client 10.0};Server=$server;Database=$database;", $user, $password);

// Microsoft Access
$connection = odbc_connect("Driver={Microsoft Access Driver (*.mdb)};Dbq=$mdbFilename", $user, $password);

// Microsoft Excel
$excelFile = realpath('C:/ExcelData.xls');
$excelDir = dirname($excelFile);
$connection = odbc_connect("Driver={Microsoft Excel Driver (*.xls)};DriverId=790;Dbq=$excelFile;DefaultDir=$excelDir" , '', '');
?>

Now you can execute any query using connection variable.

<?php 
$cur=odbc_exec($connection ,"select id,name,email from tbl_name"); 
while(odbc_fetch_row($cur))
{ 
//collect results
$id=odbc_result($cur,1); 
$name=odbc_result($cur,2); 
$email=odbc_result($cur,3);
//format and display results
    print ("<tr>");
    print ("<td>$id</td>");
    print ("<td>$name</td>");
    print ("<td>$email</td>");
    print ("</tr>");
    }

//disconnect from database
    odbc_close($connection );

?>

That’s solve, if you have any query please do comment. :) :)

About the author

PHP | MVC | Codeignitor | Zend | Yii | Smarty | Android | Laravel Expert Senior Software Developer.I love my job and feel happy to working on new ideas and technologies.

Leave a Comment

Comment (required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Name (required)
Email (required)