Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Connect to MS ACCESS db through ODBC

Status
Not open for further replies.

curvv

IS-IT--Management
Jan 11, 2001
103
ZA
Hi,

I need to connect to a MS Access database through ODBC using PHP running on Win2k server. I've always used mySQL but now need to do it with ODBC.

Can anyone please help me with the PHP ODBC connection syntax.

thanx
Cris
 
simple enough.
let's show it by an exemple
$dbname="name_of_your_odbc_datasource";
$dbuser="login_to_the_database";
$dbpass="password";

//first you have to create a connection
$conn=odbc_connect($db,$dbuser,$dbpass);

//now you can create query and access the database through odbc
$sel="select * from table";
$res=odbc_exec($conn,$sel);
odbc_result_all($res); // fast way to see all the data returned by the query
//or you can do it this way too.
while (odbc_fetch_row($res)) {
$data=odbc_result($res,1);
$data2=odbc_result($res,2);
echo $data.&quot;<BR>&quot;.$data2.&quot;<HR>&quot;;
}
odbc_free_result($res);
odbc_close($conn);


for more information check the php manual entry on unified odbc functions


Khaldryck
Programmer at the University of Brussel (Belgium)
 
Very good example and perfect.

There is also a 'database extraction layer' you can use which provides a very simple 'API' for use to connect to a large number of databases. This is for COM use utilizing both ODBC and OLEdb. This way, in the future, if you switch to Oracle, Sybase, MySQL, etc. it takes only 1-2 lines of change:

Here is a good place to download an ADODB library for you to use with your PHP applications:

//for windows
//for unix

This class library currently supports MySQL, PostgreSQL, Interbase, Oracle, MS SQL 7, Foxpro, Access, ADO, Sybase and generic ODBC. The Sybase and PostgreSQL drivers are community contributions.

<?
$dbc = new COM(&quot;ADODB.Connection&quot;);
$dbc->Provider = &quot;MSDASQL&quot;;
$dbc->Open(&quot;nwind&quot;);
$rs = $dbc->Execute(&quot;select * from products&quot;);
$i = 0;
$fld0 = $rs->Fields(0);
$fld1 = $rs->Fields(1);
$fld2 = $rs->Fields(2);
while (!$rs->EOF) {
$i += 1;
print &quot;$fld0->value $fld1->value $fld2->value<BR>&quot;;
$rs->MoveNext(); /*updates fld0, fld1, fld2 !*/
}
$rs->Close();
?>


Chad. ICQ: 54380631
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top