Scripts

Extra MySQL functies

Het spreekt voor zich... Weet je nog en leuke functie?? pm even of plaats een reactie, dan voeg ik hem toe

extra-mysql-functies
<?php
//check if a query has results
function mysql_results_empty($query){
	$result = mysql_query($query);
	if(mysql_num_rows($result) != 0){
		return true;
	}else{
		return false;
	}
}
//connect and select db
function mysql_conn($host,$user,$pass,$db){
	$conn = mysql_connect($host,$user,$pass);
	if (!$conn) {
		return false;
		exit;
	}
	
	$db_selected = mysql_select_db($db, $conn);
	if (!$db_selected) {
		return false;
		exit;
	}
	return true;
}
//returns the average of all the querty results
function mysql_average($query,$num_col,$weight = 0){
	$a = 0;
	$tot = 0;
	$result = mysql_query($query);
	while ($array = mysql_fetch_array($result)) {
		if($weight == 0){
			$tot = $tot + $array[$num_col];
			$a++;
		}else{
			$tot = $tot + $array[$num_col] * $array[$weight];
			$a = $a + $array[$weight];
		}
	}
	$gem = $tot / $a;
	return $gem;
}
//returns the id of the inserted querty
function mysql_query_id($query){
	mysql_query($query);
	return mysql_insert_id();
}
//sprintf for mysql_querty
function mysql_queryf($query) {
    if (func_num_args() > 1) {
        $args = func_get_args();
        $query = call_user_func_array("sprintf",$args);
    }
    return mysql_query($query);
}

//@ gerjo
function mysql_fetch_query($sql, $return_row = false) {
    $res = mysql_query($sql);
    return (mysql_error() != '') ? false:(($return_row) ? mysql_fetch_row($res) : mysql_fetch_assoc($res));
}
?>
		

Reacties

0
Nog geen reacties.