In what scenarios would it be recommended to use ODBC connection for querying data in PHP instead of other methods?
When working with databases that are only accessible through ODBC drivers, it would be recommended to use ODBC connection for querying data in PHP. This is often the case when working with legacy systems or connecting to databases that do not have native PHP drivers available.
<?php
$dsn = "Driver={ODBC Driver 17 for SQL Server};Server=localhost;Database=mydatabase;";
$user = "username";
$pass = "password";
$conn = odbc_connect($dsn, $user, $pass);
if ($conn) {
$query = "SELECT * FROM mytable";
$result = odbc_exec($conn, $query);
while ($row = odbc_fetch_array($result)) {
print_r($row);
}
odbc_close($conn);
} else {
die("ODBC connection failed");
}
?>