File: //home/posscale/subdomains/ecc/jason/includes/php/db.php
<?
function db_query($query, $return_type =2, $debug=true, $db_host=FALSE, $db_user=FALSE, $db_pass=FALSE, $db_name=FALSE)
{
global $config;
// connect to the database
if(!$db_host)
{
mysql_connect($config['db_host'], $config['db_username'], $config['db_password']);
@mysql_select_db($config['db_database']) or die('Database Connection Error1');
}
else
{
mysql_connect($db_host, $db_user, $db_pass);
@mysql_select_db($db_name) or die('Database Connection Error2');
}
if($return_type !=3)
{
$row['mysql_query'] = $query;
}
switch($return_type )
{
// no data returned from DB
case 0:
// else just run the query
$result = mysql_query($query);
$sql_error = mysql_error();
$row['mysql_error'] = $sql_error;
break;
// $return = 1st record, 1st column
case 1:
// run the query, and put the result into a variable
$result=mysql_query($query);
// insert the mysql error into a variable so we can check for
// an error later in the script
$sql_error=mysql_error();
$row['mysql_error'] = $sql_error;
// if sql error is empty
if($sql_error=="")
{
if(mysql_num_rows($result)>0)
{
$return = stripslashes(mysql_result($result,0,0));
}
else
{
$return = "";
}
}
break;
// data returned in default way
case 2:
// echo "MYSQL QUERY ==> Entered Case 2 \n";
// run the query, and put the result into a variable
$result = mysql_query($query);
// insert the mysql error into a variable so we can check for
// an error later in the script
$sql_error = mysql_error();
$row['mysql_error'] = $sql_error;
// if sql error is empty
if($sql_error=="")
{
//$row = mysql_fetch_array($result);
$i=0;
while($i<mysql_num_fields($result))
{
$field_name = mysql_fetch_field($result, $i);
$f_names[$field_name->name]=$field_name->name;
$i++;
}
foreach($f_names as $arr_field_name)
{
$i=0;
while($i<mysql_num_rows($result))
{
$row[$arr_field_name][$i] = stripslashes(mysql_result($result, $i, $arr_field_name));
$i++;
}
}
$row['mysql_num_rows'] = mysql_num_rows($result);
}
break;
case 3:
// run the query, and put the result into a variable
$result = mysql_query($query);
// insert the mysql error into a variable so we can check for
// an error later in the script
// $sql_error = mysql_error();
// $row['mysql_error'] = $sql_error;
// if sql error is empty
if($sql_error=="")
{
//$row = mysql_fetch_array($result);
$i=0;
while($i<mysql_num_fields($result))
{
$field_name = mysql_fetch_field($result, $i);
$f_names[$field_name->name]=$field_name->name;
$i++;
}
foreach($f_names as $arr_field_name)
{
$i=0;
while($i<mysql_num_rows($result))
{
$row[stripslashes(mysql_result($result, $i, 0))] = stripslashes(mysql_result($result, $i, 1));
$i++;
}
}
// $row['mysql_num_rows'] = mysql_num_rows($result);
}
break;
// $return = array in col=>val form, 1st record only
case 4:
// run the query, and put the result into a variable
$result = mysql_query($query);
// insert the mysql error into a variable so we can check for
// an error later in the script
$sql_error = mysql_error();
$row['mysql_error'] = $sql_error;
// if sql error is empty
if($sql_error=="")
{
//$row = mysql_fetch_array($result);
$i=0;
while($i<mysql_num_fields($result))
{
$field_name = mysql_fetch_field($result, $i);
$f_names[$field_name->name]=$field_name->name;
$i++;
}
foreach($f_names as $arr_field_name)
{
$row[$arr_field_name] = stripslashes(mysql_result($result, 0, $arr_field_name));
}
$row['mysql_num_rows'] = mysql_num_rows($result);
}
break;
}
if($debug)
{
$back_trace = debug_backtrace();
if(!empty($row['mysql_error']))
{
$_SESSION['message'][0][] = "<strong>MySQL Query:</strong> ".$back_trace[0]['line']." @ ".str_replace($config['base_dir'], '', $back_trace[0]['file'])."<br>".$query;
$_SESSION['message'][0][] = "<strong>MySQL ERROR:</strong><br>".$row['mysql_error'];
}
else
{
$_SESSION['message'][1][] = "<strong>MySQL Query:</strong> ".$back_trace[0]['line']." @ ".str_replace($config['base_dir'], '', $back_trace[0]['file'])."<br>".$query;
}
}
// close the database connection
mysql_close();
// then $return = the result variable as an array
if(empty($return))
{
$return = $row;
}
return $return;
}
function next_id($table)
{
$id = db_query("SELECT `id`
FROM `ids`
WHERE `table` ='".$table."'", 1)+1;
db_query("UPDATE `ids`
SET `id` ='".$id."'
WHERE `table` ='".$table."'", 0);
return $id;
}
function db($data)
{
return addslashes($data);
}
function password($password)
{
return md5(md5(md5($password)));
}
function str2search($search_cols, $strings)
{
$and_str = true;
foreach($strings as $str)
{
if($and_str)
{
$where .= " (";
$and_str = false;
}
else
{
$where .= " AND (";
}
$or_str = true;
foreach($search_cols as $col)
{
if($or_str)
{
$where .= "$col LIKE '%$str%'";
$or_str = false;
}
else
{
$where .= " OR $col LIKE '%$str%'";
}
}
$where .= ")";
}
return $where;
}
function do_quick_search($query, $search_cols, $query_postfix='', $limit=5, $page = 1, $upper_limit=150)
{
global $config;
$where = str2search($search_cols, explode(" ", $_POST['str']));
if(isset($_POST['page']))
{
$page = $_POST['page'];
}
$sql = $query.$where.$query_postfix." LIMIT ".$upper_limit;
$results = db_query($sql);
$result_total = $results['mysql_num_rows'];
$show_from = (($page-1) * $limit);
if($show_from < 1) { $show_from = 0; }
$show_to = ($page * $limit);
if($show_to > $result_total) { $show_to = $result_total; }
if($show_from > 1)
{
$previous_page = html_a('#', html_img($config['theme_url'].'/'.$config['theme'].'/images/pager_prev.gif'), 1, 'onclick|post(\''.$config['base_url'].'/'.$_GET['uri'].'\', \'action='.$_POST['action'].'&str='.$_POST['str'].'&page='.($page-1).'\', \'update_list\'); return false;');
$first_page = html_a('#', html_img($config['theme_url'].'/'.$config['theme'].'/images/pager_first.gif'), 1, 'onclick|post(\''.$config['base_url'].'/'.$_GET['uri'].'\', \'action='.$_POST['action'].'&str='.$_POST['str'].'&page='.(1).'\', \'update_list\'); return false;');
}
else
{
$previous_page = html_img($config['theme_url'].'/'.$config['theme'].'/images/pager_prev_0.gif');
$first_page = html_img($config['theme_url'].'/'.$config['theme'].'/images/pager_first_0.gif');
}
if($show_to < $result_total)
{
$next_page = html_a('#', html_img($config['theme_url'].'/'.$config['theme'].'/images/pager_next.gif'), 1, 'onclick|post(\''.$config['base_url'].'/'.$_GET['uri'].'\', \'action='.$_POST['action'].'&str='.$_POST['str'].'&page='.($page+1).'\', \'update_list\'); return false;');
$last_page = html_a('#', html_img($config['theme_url'].'/'.$config['theme'].'/images/pager_last.gif'), 1, 'onclick|post(\''.$config['base_url'].'/'.$_GET['uri'].'\', \'action='.$_POST['action'].'&str='.$_POST['str'].'&page='.ceil($result_total/$limit).'\', \'update_list\'); return false;');
}
else
{
$next_page = html_img($config['theme_url'].'/'.$config['theme'].'/images/pager_next_0.gif');
$last_page = html_img($config['theme_url'].'/'.$config['theme'].'/images/pager_last_0.gif');
}
$results_total_post = '';
if($results['mysql_num_rows']>=$upper_limit)
{
$results_total_post = '+';
}
$pager = html_div(html_div('Results '.($show_from + 1).' to '.$show_to.' of '.$result_total.$results_total_post, 'class|pager_details').html_div($first_page.$previous_page.$next_page.$last_page, 'class|pager_controls'), 'class|pager_container').html_clearb();
return array(
'results'=>$results,
'pager'=>$pager,
'show_from'=>$show_from,
'show_to'=>$show_to,
);
}
?>