Back to Top

PHP ODBC connection with MySQL

PHP ODBC connection with Mysql

MySQL database is usually used to store data for PHP application and data are stored using the MySQL configuration and connection.Today I am going to cover a topic which is to connect PHP and MySQL using ODBC connection as from last two-three days I was working on it.

To make ODBC connection with MySQL tables, First of all you need to install MySQL ODBC drivers that is Connector-ODBC and you can get ODBC drivers from Once MySQL ODBC connection driver installed in your PC, you need to configure it.

To Configure MySQL ODBC connection follow the below steps:

  1. Go to Contral Panel >> Administrative Tools and then click Data Sources (ODBC)
  2. To open the ODBC Data Source Administrator
  3. Choose the System DSN tab
  4. Click on Add in the System DSN tab
  5. Select MySQL ODBC 5.1 Driver, then click Finish
  6. Now need to configure the specific fields Data source Name for the DSN.
  7. Click OK to save the DSN

Let’s have a look


In above box, enter the name of the data source(database) you want to access and in the Server field, enter the name of the MySQL hostname that you want to access(e.g localhost) then in user and password field enter database username and password.

Also, Read MySQL Connection: Let be Assured and Database BackUp Script

Now you are connected with ODBC and you can write PHP code for odbc connection with MySQL.

The odbc_connect() function is used to connect to an ODBC data source and The odbc_exec() function is used to execute an SQL statement.

$conn = odbc_connect('data source name','username','password');

After connecting with ODBC, The odbc_fetch_row() function is used to return records from the result-set. This function returns true if it is able to return rows, otherwise false


Then, To retrieve fields The odbc_result() function is used to read fields from a record.

$firstname = odbc_result($resultset,"FirstName"); 

Let’s take one Example which show data after ODBC connection:

// First parameter is your DSN name, second is database username and third is database password

$sql = "SELECT * FROM users";
$rs = odbc_exec($conn,$sql);

while (odbc_fetch_row($rs)) {
   $firstname = odbc_result($rs,"FirstName"); // second parameter contain a column name 
   $lastname = odbc_result($rs,"LastName"); 


	First Name Last Name
	Bhumi       Shah
	Zinal       Shah

If you have any query or confusion in above article, Do let me know.Have you developed any application using ODBC, share your thoughts in below comments section.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

How to use Namespace in PHP

Posted on 7 years ago


Learn about the Docker and how it works

Posted on 2 years ago


How to Get IP address in CodeIgniter

Posted on 6 years ago


AWS Cloud Database Migration Service

Posted on 2 years ago


The Reader’s Poll – June 2015

Posted on 3 years ago


The Reader’s Poll – August 2014

Posted on 4 years ago


The Reader’s Poll – June 2014

Posted on 4 years ago


The Readers’ Poll – May 2014

Posted on 5 years ago


The Readers’ Poll – August 2013

Posted on 5 years ago