The description of odbc_data_source is misleading. It says "Returns information about a current connection" but in actual fact, it returns all possible DSNs in your server (on subsequent calls) ...and the first call doesn't even return the description of the current connection at all example:
$dsn = "DRIVER={MySQL ODBC 5.1 Driver};Server=localhost;Database=mysql";
$conn = odbc_connect($dsn, 'root', '') or die(odbc_errormsg() );
$result = @odbc_data_source($conn, SQL_FETCH_FIRST) ;
var_dump($result);
odbc_close($conn);
outputs the following on my machine:
array(2) { ["server"]=> string(11) "dBASE Files" ["description"]=> string(51) "Microsoft Access dBASE Driver (*.dbf, *.ndx, *.mdx)" }
Which is not one would expect (or am I wrong?). I would expect 'server' to return 'MySQL' since that is the DSN I used to connect.
Is there any way I can get the data source info of the current connection I am in?
Thanks.
odbc_data_source
(PHP 4 >= 4.3.0, PHP 5)
odbc_data_source — Returns information about a current connection
Description
array odbc_data_source
( resource $connection_id
, int $fetch_type
)
This function will return the list of available DNS (after calling it several times).
Parameters
- connection_id
-
The ODBC connection identifier, see odbc_connect() for details.
- fetch_type
-
The fetch_type can be one of two constant types: SQL_FETCH_FIRST, SQL_FETCH_NEXT. Use SQL_FETCH_FIRST the first time this function is called, thereafter use the SQL_FETCH_NEXT.
Return Values
Returns FALSE on error, and an array upon success.
odbc_data_source
benjie
03-Dec-2009 07:47
03-Dec-2009 07:47
critmas at hotmail dot com
25-Feb-2005 07:53
25-Feb-2005 07:53
Thank you Angelo for point out the code. Though the function doesn't really do what it is supposed to. It returns the entire list of ODBC DSNs.
If you really are looking to get the server type based on just the ODBC, username and password in an environment where the application needs to be aware of different types of databases, use the following code:
$link2 = odbc_connect($dsn , $DBUser, $DBPwd ) or die(odbc_errormsg() );
$result = @odbc_data_source( $link2, SQL_FETCH_FIRST );
while($result)
{
if (strtolower($dsn) == strtolower($result['server'])) {
echo $result['description'] . "<br>\n";
break;
}
else
$result = @odbc_data_source( $link2, SQL_FETCH_NEXT );
}
odbc_close($link2);
// Hope it saves your precious time
angelo [at] spaceblue [dot] com
01-Dec-2003 06:51
01-Dec-2003 06:51
// Example usage:
// Connect to a ODBC database that exists on your system
$link = odbc_connect("some_dsn_name", "user", "password") or die(odbc_errormsg() );
$result = @odbc_data_source( $link, SQL_FETCH_FIRST );
while($result)
{
echo "DSN: " . $result['server'] . " - " . $result['description'] . "<br>\n";
$result = @odbc_data_source( $link, SQL_FETCH_NEXT );
}
odbc_close($link);
