Location: PHPKode > projects > Homeless Mangement Information System > hmis/include/db_connection_mssql.inc
<?php

function db_generic_connect ()  {
    GLOBAL $db_server_address, $db_name, $db_login, $db_password;
    
    $db_link = mssql_connect($db_server_address, $db_login, $db_password) or die("Couldn't connect to SQL Server on $myServer");
    mssql_select_db ( $db_name );
    return $db_link;

}



function run_query ($sql, $error_message)	{
	GLOBAL $db_link, $transaction_in_progress;

    //echo "<br/>_________________<br/><span class=\"tiny\">Query: ".$error_message.'</span>'; echo '<pre>'.$sql.'</pre>'; $timer = new Script_timer();
    
    $sql = str_replace("\'", "''", $sql);
    $query_result = mssql_query($sql);
    if(!$query_result)  {
        
        if ( $transaction_in_progress ) {
            transaction_rollback ("Failed query caused rollback of transaction<p/>");    
        }
        
        echo "DB SQL Query Error: ".$error_message." ".getenv("SCRIPT_NAME")." ".$sql."<p/>";
        exit;
    }	

	//$ex_time = $timer->Get_Time(4); echo "<span class=\"tiny\">Seconds to execute: ".$ex_time.' -- Grand total seconds: '.$timer->total_time.'</span>';
    
    return $query_result;

}



function fetch_array ($query_result, $error_message, $row_num="5432111")	{

	//$row_array =  mysql_fetch_array ($query_result);
    mssql_data_seek ( $query_result, $row_num);
	$row_array =  mssql_fetch_array ($query_result);
	if(!$row_array)	{echo "DB Fetch Array Error: ".$error_message." ".getenv("SCRIPT_NAME"); exit; }
	return $row_array;
    if($row_num="5432111"){echo "No ROW number<br/>";}
}



function num_rows ($query_result)	{

	//$row_count =  mysql_num_rows ($query_result);
    //$row_count =  pg_numrows ($query_result);
    $row_count =  mssql_num_rows ($query_result);
	return $row_count;
}



function fetch_result ($query_result, $error_message)	{

	//$row_result_raw =  mysql_result ($query_result, 0);
    $row_result_raw =  mssql_fetch_array ($query_result);
	if(!$row_result_raw)	{echo "DB Fetch Result Error: ".$error_message." ".getenv("SCRIPT_NAME"); exit; }
	
    //$row_result = $row_result_raw; //MySQL only
    $row_result = $row_result_raw[0];//postgreSQL only    
    
    return $row_result;
}



function fetch_assoc ($query_result, $error_message, $row_num="5432")	{

	//$row_array =  mysql_fetch_assoc ($query_result);
    $row_array =  pg_fetch_array ($query_result, $row_num, "PGSQL_ASSOC");
	if(!$row_array)	{echo "DB Fetch Array Error: ".$error_message." ".getenv("SCRIPT_NAME"); exit; }
	return $row_array;
    if($row_num="5432"){echo "No ROW number<br/>";}
}



function run_query_return_array ($sql, $error_message, $use_array_key="")  {
    //GLOBAL $db_link;
    //echo $sql."<p/>";
    $query_result = mssql_query ($sql);
    if(!$query_result)	{echo "DB SQL run_query_return_array Error: ".$error_message."
    ".getenv("SCRIPT_NAME")." ".$sql; exit; }	
    $row_count = mssql_num_rows ($query_result);
    
    //If a key is specified, use it to assemble the array to be returned
    if($use_array_key)  {
        for ( $i = 0; $i < $row_count; $i++ )    {
            mssql_data_seek ( $query_result, $i);
            $temp_return_array = mssql_fetch_array ($query_result);
            $return_array[$temp_return_array[$use_array_key]] = $temp_return_array;   
        }
    
    }
    
    else {
    
        for ( $i = 0; $i < $row_count; $i++ )    {
            mssql_data_seek ( $query_result, $i);
            $return_array[$i] = mssql_fetch_array ($query_result);   
        }
    }

    return $return_array;
}



function run_query_return_single_row ($sql, $error_message) {
	GLOBAL $db_link;
    //echo $sql."<p/>";
    $query_result = mssql_query ($sql);
    if(!$query_result)	{echo "DB SQL run_query_return_array Error: ".$error_message."
    ".getenv("SCRIPT_NAME")." ".$sql; exit; }	
    if (mssql_num_rows ($query_result) > 0) {  
    	$return_array = mssql_fetch_array ($query_result);   
	}
	
    return $return_array;	

}



function return_column_names_array ($table_name, $offset=0)	{

	$result = run_query("SELECT attname FROM pg_attribute, pg_type WHERE typrelid=attrelid AND typname = '".$table_name."'",
     "Column Names Not Retreived from ".$table_name);
    
    $row_count = num_rows ($result);
    
    settype($returned_array["array"], "array");
    
    for($i=7+$offset; $i < $row_count; $i++)    {
        $raw_array = fetch_array($result, "Column name in table ".$table_name." not fetched", $i);
        $first_array_result = $raw_array[0];
        $returned_array["list"] .= $first_array_result;
        if ($i != $row_count-1)   {
        $returned_array["list"] .= ", ";
        }
        array_push($returned_array["array"], $first_array_result); 
    }
    $returned_array["column_count"] = count($returned_array["array"]);
    
    return $returned_array;       
}



function transaction_begin ( $error_message )   {
    GLOBAL $transaction_in_progress, $transaction_in_progress_count;
    if ( strlen ( $transaction_in_progress ) > 0 ) {
        //echo 'Transaction added to: '.$error_message.' already in progress<p/>';
        $transaction_in_progress = $transaction_in_progress.", ".$error_message;
        $transaction_in_progress_count++;
    }
    else {
        run_query ( 'BEGIN TRANSACTION', $error_message );
        //echo 'NEW Begin'.$error_message.'<br/>';
        $transaction_in_progress = $error_message;
        $transaction_in_progress_count = 1;
    }

}



function transaction_verify ( $error_message )  {
    GLOBAL $transaction_in_progress;
    if ( !$transaction_in_progress )    {
        //echo 'There is no transaction encompassing: '.$error_message.'<p/>';
        $ret_val = -1;
    }
    else {
        $ret_val = 1;    
    }
    
    return $ret_val;

} 



function transaction_rollback ($error_message)   {
    GLOBAL $transaction_in_progress;    
    //echo 'The following transaction rolled back: '.$transaction_in_progress.'; Error message:
    //'.$error_message.'<p/>';;
    run_query ( 'ROLLBACK', 'Error rolling back: '.$transaction_in_progress );
    $transaction_in_progress = '';
    $transaction_in_progress_count = '';

}



function transaction_commit ()   {
    GLOBAL $transaction_in_progress, $transaction_in_progress_count;
    //echo 'Commit'.$transaction_in_progress.'<br/>';
    $transaction_in_progress_count--;
    
    if ( $transaction_in_progress_count == 0 )  {
    
        run_query ( 'COMMIT', $error_message );
        //unset ( $transaction_in_progress );
        $transaction_in_progress = '';
        $transaction_in_progress_count = 0;
        //echo 'ACUTAL Commit'.$transaction_in_progress.'<br/>'; 
    }
    
}



class Script_Timer {
    var $start_timea;
    var $total_time;   
    
   function Script_Timer ()
   {
       $z = explode (' ', microtime());
       $z = $z[1] + $z[0];
       
       define ('TIMER_START_TIME', $z);
       $this->start_timea = microtime ();
      
       return $start_time;
   }
  
   function Get_Time ($decimals=2)
   {
       // $decimals will set the number of decimals you want for your milliseconds.

       // format start time
       $start_time = explode (' ', $this->start_timea);
       $start_time = $start_time[1] + $start_time[0];
       // get and format end time
       $end_time = explode (' ', microtime());
       $end_time = $end_time[1] + $end_time[0];
       $this->total_time = number_format ($end_time - TIMER_START_TIME, $decimals);
       $h = $end_time - $start_time;
       
       $i = number_format ($h, $decimals);
       if ( $h > 1 )    {
        $i = "<span class=\"headline\">".$i."</span>";
       }
       return $i;
   }
}


?>
Return current item: Homeless Mangement Information System