PHP and ODBC

 

Using PHP to connect database via ODBC is not popular but possible. You can find the reference of built-in function of PHP and ODBC in the following url:

http://www.php.net/manual/en/ref.uodbc.php

Build a connection

<?php
//Connect to database by specify ODBC name and username and password
$conn = odbc_connect("odbc_name", "username", "password");
?>

Retrieve data from database

//Query the database
$sql = "SELECT username FROM users 
                          WHERE username='username' and password='password'";
$result = odbc_exec($conn, $sql);

//Test the query result
$user = odbc_result($result, "username");
echo("Welcome: ".$user);

Add new record

The following code show how to insert the record

//create sql insert statement
$sql = "INSERT INTO users (username, password) VALUES('Ruby', 'r0012')";
//execute query
$result = odbc_exec($conn, $sql);

Update record

The following code show how to update the record

//create sql update statement
$sql = "UPDATE users SET password='rNewPwd' WHERE username='Ruby'";
//execute query $result = odbc_exec($conn, $sql);

Delete record

The following code show how to delete the record

//create sql delete statement
$sql = "DELETE FROM users WHERE username='Ruby'";
//execute query
$result = odbc_exec($conn, $sql);

Close connection

Make the code complete by close the connection.

odbc_close($conn);
 
Visitor Review  Write a review
Search Review :

Rob Miller on 20 Jan 2010 04:15 reply

None./

 

MikeFigueroa.com on 02 Jan 2009 22:50 reply

You could use the odbc_result_all() to output the result of a select query, but if you want to work with the data you have selected, you could use a loop to fill an array:

$sql = "SELECT date, sales FROM sales ORDER BY date";
$result = @odbc_exec($connection, $sql);
$sales = array();
$date = array();
$i = 0;
while ($row = @odbc_fetch_array($result)) {
$temp = $row['date'];
$date[$i] = $temp;
$temp = $row['sales'];
$sales[$i] = $temp;
$i++;
}

Now you can odbc_close_all() & access/manipulate the data at your leisure such as:
$strongest_sales = max($sales);
or
$earliest_date = $date[0];

 
See all reviews
Write a review
Rating:
Your Message:
Name:
Email:
(optional)
(Your email that entered here will not show anywhere on website and will use only reference when someone reply your post)
  I want to receive a copy of email on this post (your email is needed)
 
Code:
 

Search this site