<?php

/**
 * @page connexion_mssqlserver.php
 * @brief Exemple de connexion à une base Microsoft SQL Server (2 méthodes)
 *
 * @author hughes monget
 * @see http://monget.com/
 */

/** Avec SQL Server Driver for PHP
 *
 * @see http://www.microsoft.com/sqlserver/2005/en/us/php-driver.aspx
 */

error_reporting(0);

highlight_file(__FILE__);

echo 
'<hr />';

echo 
'<pre>';

if (
extension_loaded('sqlsrv'))
{
    echo 
'<a href="http://www.microsoft.com/sqlserver/2005/en/us/php-driver.aspx" target="_blank">SQL Server Driver for PHP</a>'."\n\n";

    
$str_serveur = 'HUGHES2009\SQLEXPRESS';
    
$arr_str_connexion = array('Database'=>'GESTION', 'UID' => ':-)', 'PWD' => ':-)');
    
$res_connexion = sqlsrv_connect($str_serveur, $arr_str_connexion);
    if (
$res_connexion === false)
    {
        echo 
'Could not connect.';
        exit(
print_r(sqlsrv_errors(), true));
    }

    
$str_sql = 'SELECT * FROM T_UTILISATEUR';
    
$res_stmt = sqlsrv_query( $res_connexion, $str_sql);
    if( 
$res_stmt === false)
    {
        echo 
'Error in query preparation/execution.';
        exit(
print_r(sqlsrv_errors(), true));
    }

    while (
$arr_mix = sqlsrv_fetch_array($res_stmt, SQLSRV_FETCH_NUMERIC))
    {
        echo 
implode('|', $arr_mix).'<br />';
    }

    
sqlsrv_free_stmt($res_stmt);
    
sqlsrv_close($res_connexion);
}

echo 
'<hr />';

if (
extension_loaded('mssql'))
{
    echo 
'<a href="http://www.php.net/manual/fr/book.mssql.php" target="_blank">Module mssql</a>'."\n\n";

    
$res_connexion = mssql_connect('HUGHES2009\SQLEXPRESS', 'gestion', '2002mp2002');
    if (
$res_connexion === false)
    {
        echo 
'Could not connect.';
        exit(
print_r(mssql_get_last_message(), true));
    }

    if (
mssql_select_db('GESTION', $res_connexion) === false)
    {
        echo 
'Could not select database.';
        exit(
print_r(mssql_get_last_message(), true));
    }

    
$str_sql = 'SELECT * FROM T_UTILISATEUR';
    
$res_stmt = mssql_query($str_sql, $res_connexion);
    if( 
$res_stmt === false)
    {
        echo 
'Error in query preparation/execution.';
        exit(
print_r(mssql_get_last_message (), true));
    }

    while (
$arr_mix = mssql_fetch_array($res_stmt, MSSQL_NUM))
    {
        echo 
implode('|', $arr_mix).'<br />';
    }

    
mssql_free_result($res_stmt);
    
mssql_close($res_connexion);

}

echo 
'</pre>';