'How to check if my ODBC Data Source exist in PHP?

I have a simple project and that is to create a function that will check for mysql and odbc connection. I'm already done in creating the function for mysql, here's my sample code:

function check() {
    $serverName = 'localhost';
    $userName = 'root';
    $password = '123';
    $db = 'sample';

    $conn = mysql_connect($serverName, $userName, $password);
    mysql_select_db($db, $conn);

    $trans = 'SELECT * FROM Labels';
    $trans_result = mysql_query($trans, $conn);

    if(!$trans_result) {
        die(mysql_error());
    } else {
        echo "connected";
    }
}

Well this one works for me when checking for the mysql connection. Now, my question is, is it possible to create something like this for checking my odbc data source connection? So that would be like

$conn = odbc_connect("spmuse1","" ,""); # Open connection.

$trans = "SELECT French FROM Labels";
$trans_result = odbc_exec($conn, $trans);

if(!$trans_result) {
    echo "error?";
} else {
    echo "connected";
}

You know what I mean? When I use this code, I always have 2 this error

Warning: odbc_connect() [function.odbc-connect]: SQL error: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified, SQL state IM002 in SQLConnect

Warning: odbc_exec(): supplied argument is not a valid ODBC-Link resource

Please help! Thanks.



Solution 1:[1]

First you need to decide vendor of odbc driver, I hope below example will works for you

  <?php

     // Configure connection parameters
    $db_host        = "server.mynetwork";
    $db_server_name = "Dev_Server";
    $db_name        = "Dev_Data";
    $db_file        = 'c:\dbstorage\dev.db';
    $db_conn_name   = "php_script";
    $db_user        = "dbuser";
    $db_pass        = "dbpass";

    $connect_string = "Driver={Adaptive Server Anywhere 8.0};".
                "CommLinks=tcpip(Host=$db_host);".
                "ServerName=$db_server_name;".
                "DatabaseName=$db_name;".
                "DatabaseFile=$db_file;".
                "ConnectionName=$db_conn_name;".
                "uid=$db_user;pwd=$db_pass";

    // Connect to DB
    $conn = odbc_connect($connect_string,'','');

    // Query
    $qry = "SELECT * FROM my_table";

    // Get Result
   $trans_result= odbc_exec($conn,$qry);

  if(!$trans_result) {
    echo "error?";
  } else {
    echo "connected";
  }
?>

Solution 2:[2]

I spent several days looking for a simple answer, and came up with this, which works for me:

if (@odbc_connect("DBName","un","pw",SQL_CUR_USE_ODBC) == FALSE){
    echo "Database does not exist";
} else {
    $connection=odbc_connect("DBName","un","pw",SQL_CUR_USE_ODBC);
    echo "Database exists";
}

The @ suppresses the basic error if the database does not exist, so the connection try will just return false. Of course if the connection is good, then it creates the connection object.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 AMIC MING
Solution 2 Hanna