Frontaccounting connect_db.inc Upgrade |
X

Congrats, You are Subscribed to Receive Updates.

Frontaccounting connect_db.inc Upgrade


Frontaccounting connect_db.inc Upgrade. The existing function of connect_db using mysql_connect( mysql function()  will be removed soon). So, we must upgrade to mysqli_connect or PDO.   Here i just upgraded to mysqli connect. Frontaccouting with awesome functions are so awesome. why dont you to upgrade the mysql function to mysqli function which will help you to work on latest version of PHP.   Just copy paste the following code to your connect_db.inc.

*** NOTE: Core update of version 2.4 Rc1 has been updated with this functions. If you are working with latest version of Frontaccounting.  dont use it

Just goto the root of your frontaccounting files, than followed by  “includes“, than “db“. Here you can see the connect_db.inc file, just open i ton your text editor and replace with the below code.

<?php
/*		   ______________________________________
  ________|                                      |_______
  \       |    FrontAccotuning Customization     |		/
   \      |    By Kvvaradha			  			 |     /
   /      |______________________________________|     \
  /__________)                                (_________\   */

function set_global_connection($company=-1){
	global $db, $transaction_level, $db_connections;

	cancel_transaction(); // cancel all aborted transactions if any
	$transaction_level = 0;

	if ($company == -1) 
		$company = $_SESSION["wa_current_user"]->company;

	$_SESSION["wa_current_user"]->cur_con = $company;

	$connection = $db_connections[$company];

	$db = mysqli_connect($connection["host"], $connection["dbuser"], $connection["dbpassword"], $connection["dbname"]);
		if (strncmp(mysqli_get_server_info($db), "5.6", 3) >= 0) 
		db_query("SET sql_mode = ''");
	/////
	return $db;
}

$db_duplicate_error_code = 1062;

//DB wrapper functions to change only once for whole application

function db_query($sql, $err_msg=null){
	global $db, $show_sql, $sql_trail, $select_trail, $go_debug, $sql_queries, $Ajax,
		$db_connections, $db_last_inserted_id;
	
	// set current db prefix
	$cur_prefix = $db_connections[$_SESSION["wa_current_user"]->cur_con]['tbpref'];
	$sql = str_replace(TB_PREF, $cur_prefix, $sql);

	if ($show_sql){
		$Ajax->activate('footer_debug');
		$sql_queries .= "<pre>$sql</pre>\n<hr>";
	}

	$result = mysqli_query($db, $sql);
	
	if($sql_trail) {
		$db_last_inserted_id = mysqli_insert_id($db);	// preserve in case trail insert is done
		if ($select_trail || (strstr($sql, 'SELECT') === false)) {
			mysqli_query($db,
			"INSERT INTO ".$cur_prefix."sql_trail
				(`sql`, `result`, `msg`)
				VALUES(".db_escape($sql).",".($result ? 1 : 0).",
				".db_escape($err_msg).")");
		}
	}

	if ($err_msg != null || $go_debug) {
		$exit = $err_msg != null;
		if (function_exists('xdebug_call_file'))
			check_db_error('<br>At file '.xdebug_call_file().':'.xdebug_call_line().':<br>'.$err_msg, $sql, $exit);
		else
			check_db_error($err_msg, $sql, $exit);
	}
	return $result;
}

function db_fetch_row ($result){	return mysqli_fetch_row($result);	}

function db_fetch_assoc ($result){	return mysqli_fetch_assoc($result);	}

function db_fetch ($result){	return mysqli_fetch_array($result, MYSQLI_BOTH);	}

function db_seek (&$result,$record){	return mysqli_data_seek($result, $record);	}

function db_free_result ($result){
	if ($result)
		mysqli_free_result($result);
}

function db_num_rows ($result){	return mysqli_num_rows($result);}

function db_num_fields ($result){	return mysqli_num_fields($result);}

function db_escape($value = "", $nullify = false){
	global $db;
	$value = @html_entity_decode($value, ENT_QUOTES, $_SESSION['language']->encoding);
	$value = @htmlspecialchars($value, ENT_QUOTES, $_SESSION['language']->encoding);

  	//reset default if second parameter is skipped
	$nullify = ($nullify === null) ? (false) : ($nullify);

  	//check for null/unset/empty strings
	if ((!isset($value)) || (is_null($value)) || ($value === "")) {
		$value = ($nullify) ? ("NULL") : ("''");
	} else {
		if (is_string($value)) {
      		//value is a string and should be quoted; determine best method based on available extensions
			if (function_exists('mysqli_real_escape_string')) {
		  		$value = "'" . mysqli_real_escape_string($db, $value) . "'";
			} else {
			  $value = "'" . mysqli_escape_string($db, $value) . "'";
			}
		} else if (!is_numeric($value)) {
			//value is not a string nor numeric
			display_error("ERROR: incorrect data type send to sql query");
			echo '<br><br>';
			exit();
		}
	}
	return $value;
}

function db_error_no (){
	global $db;
	return mysqli_errno($db);
}

function db_error_msg($conn){	return mysqli_error($conn);}

function db_insert_id(){
	global $db_last_inserted_id, $sql_trail, $db;

	return $sql_trail ? $db_last_inserted_id : mysqli_insert_id($db);
}

function db_num_affected_rows(){
	global $db;
	return mysqli_affected_rows($db);
}

function db_field_name($result, $n){	return mysqli_fetch_field($result);	}

function db_create_db($connection){
	$db = mysqli_connect($connection["host"] ,
		$connection["dbuser"], $connection["dbpassword"]);
	if (!mysqli_select_db($db, $connection["dbname"]))
	{
		$sql = "CREATE DATABASE IF NOT EXISTS " . $connection["dbname"] . "";
		if (!mysqli_query($db, $sql) || !mysqli_select_db($db, $connection["dbname"]))
			return 0;
	}
	return $db;
}

function db_drop_db($connection){

	if ($connection["tbpref"] == ""){
		$sql = "DROP DATABASE IF EXISTS " . $connection["dbname"] . "";
		return mysqli_query($db, $sql);
	}else{
    	$res = db_query("show table status");
    	$all_tables = array();
    	while($row = db_fetch($res))
    		$all_tables[] = $row;
        // get table structures
		foreach ($all_tables as $table)	{
			if (strpos($table['Name'], $connection["tbpref"]) === 0)
				db_query("DROP TABLE `".$table['Name'] . "`");
		}
		//deleting the tables, how??
		return true;
	}
}

function db_close($dbase = null){
	global $db;
	
	if (!$dbase)
		$dbase = $db;
	return mysqli_close($dbase);
}

?>

 

If you can’t copy paste the code, just download the connect_db.inc and replace with yours.

Download Source
commenter

About Varadharaj V

The founder of Kvcodes, Varadharaj V is an ERP Analyst and a Web developer specializing in WordPress(WP), WP Theme development, WP Plugin development, Frontaccounting(FA), Sales, Purchases, Inventory, Ledgers, Payroll & HRM, CRM, FA Core Customization, PHP and Data Analyst. Database Management Advance Level

2 comments

  1. commenter

    Dear Kvvaradha,

    Thank you for your rewriting to mysqli.
    Happy camper over here!

    Best regards,
    Mike

Reply to Varadharaj V Cancel reply

Your email address will not be published. Required fields are marked *

*

Current ye@r *

Menu

Sidebar