<?php

error_reporting(E_ALL & !E_NOTICE);

$conn = mysql_connect('localhost', 'root', 'etllpb@7789') or die('Connection fail: '.mysql_error());
//mysql_set_charset('utf8',$conn);
//mysql_select_db('etl_doc');
mysql_select_db('etldoc_08022017');

/*----------- function Execute query --------------*/

mysql_query("SET NAMES 'utf8'");
/**
 * Query from the database and returns the result in array or associative array based on a primary key
 * 
 * @param string $qry Query to be executed. For example, "SELECT * FROM product"
 * @param string $pkname [optional] The name of a Primary Key to be used as an associative array's key. For example "ID"
 * @return array Result of the query if format $result[RowNumber][ColumnName]=Value or $result[PrimaryKey][ColumnName]=Value. Returns a blank array if there are no result.   
 */
 
 function nr_query($qry, $pkname = null){
	$res = mysql_query($qry);
	if ($res != null){
		$tables = array();
		while ($row = mysql_fetch_array($res, MYSQL_ASSOC)){
			if ($pkname)
				$tables[$row[$pkname]] = $row;
			else
				$tables[] = $row;
		}
	}
	return $tables;
	
}
/*----------------- Normal Function */
function sql_execute($qry){
	$res = mysql_query($qry);
	//echo $qry;
	if ($res == false){
		//echo "MySQL Error: " . mysql_error();
		return;
	}
	if ($res != null){
		$row = @mysql_fetch_row($res);	
		if ($row != null){
			return $row[0];
		}
	}
}

/**
 * Execute a query and return the first value
 * 
 * @param string $qry Query to be executed. For example, "SELECT name FROM product WHERE id=1"
 * @return mixed the first value (the first row & column) returned from the query. returns null if there is no result or incorrect query     
 */
function nr_execute($qry){
	$res = mysql_query($qry);
	if ($res != null){
		$row = @mysql_fetch_row($res);
		if ($row != null){
			return $row[0];
		}
	}
}
