PHP Velho Oeste 2024

odbc_data_source

(PHP 4 >= 4.3.0, PHP 5, PHP 7, PHP 8)

odbc_data_sourceВозвращает информацию о доступных DSN

Описание

odbc_data_source(resource $odbc, int $fetch_type): array|false

Эта функция вернёт список доступных DSN (после нескольких вызовов).

Список параметров

odbc

Идентификатор соединения ODBC, за подробностями обращайтесь к odbc_connect().

fetch_type

fetch_type может быть одним из двух типов констант: SQL_FETCH_FIRST, SQL_FETCH_NEXT. Используйте SQL_FETCH_FIRST при первом вызове этой функции, после этого используйте SQL_FETCH_NEXT.

Возвращаемые значения

Возвращает false в случае возникновения ошибки, массив (array) в случае успешного выполнения и null после выборки последнего доступного DSN.

Примеры

Пример #1 Перечисление доступных DSN

<?php
$conn
= odbc_connect('dsn', 'user', 'pass');
$dsn_info = odbc_data_source($conn, SQL_FETCH_FIRST);
while (
$dsn_info) {
print_r($dsn_info);
$dsn_info = odbc_data_source($conn, SQL_FETCH_NEXT);
}
?>

Вывод приведённого примера будет похож на:

Array
(
    [server] => dsn
    [description] => ODBC Driver 17 for SQL Server
)
Array
(
    [server] => other_dsn
    [description] => Microsoft Access Driver (*.mdb, *.accdb)
)
add a note add a note

User Contributed Notes 2 notes

up
1
angelo [at] spaceblue [dot] com
20 years ago
// 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);
up
-1
critmas at hotmail dot com
19 years ago
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
To Top