Ver Mensaje Individual
  #1 (permalink)  
Antiguo 06/04/2013, 23:32
aldelcon
 
Fecha de Ingreso: abril-2013
Mensajes: 3
Antigüedad: 13 años, 5 meses
Puntos: 0
Suma de campos mysql php resultado de una busqueda

De antemano tengo que decir que estoy apenas empezando, y agradecere cualquier ayuda.
A ver si alguien me puede echar una mano, hago una archivo busqueda.php y uno resultado.php en este ultimo una tabla dinamica con los resultados de la busqueda,
el asunto es que quiero sumar los valores de un campo, pero me suma todos los registros y no los del resultado de la busqueda:


Código PHP:
if (!function_exists("GetSQLValueString")) {
function 
GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (
PHP_VERSION < 6) {
    
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch (
$theType) {
    case 
"text":
      
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case 
"long":
    case 
"int":
      
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case 
"double":
      
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case 
"date":
      
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case 
"defined":
      
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return 
$theValue;
}
}

$colname_recibos = "-1";
if (isset(
$_GET['comercio'])) {
  
$colname_recibos = $_GET['comercio'];
}
$colname2_recibos = "-1";
if (isset(
$_GET['fechapago'])) {
  
$colname2_recibos = $_GET['fechapago'];
}
mysql_select_db($database_usersconexion, $usersconexion);
$query_recibos = sprintf("SELECT * FROM tbrecibos WHERE comercio LIKE %s AND fechapago  LIKE %s", GetSQLValueString("%" . $colname_recibos . "%", "text"),GetSQLValueString("%" . $colname2_recibos . "%", "text"));
$recibos = mysql_query($query_recibos, $usersconexion) or die(mysql_error());
$row_recibos = mysql_fetch_assoc($recibos);
$totalRows_recibos = mysql_num_rows($recibos);$colname_recibos = "-1";

if (isset(
$_GET['comercio'])) {
  
$colname_recibos = $_GET['comercio'];
}
$colname2_recibos = "-1";
if (isset(
$_GET['fechapago'])) {
  
$colname2_recibos = $_GET['fechapago'];
}
 
$total = 0 ;
mysql_select_db($database_usersconexion, $usersconexion);
$query_recibos = sprintf("SELECT * FROM tbrecibos WHERE comercio LIKE %s AND fechapago  LIKE %s", GetSQLValueString("%" . $colname_recibos . "%", "text"),GetSQLValueString("%" . $colname2_recibos . "%", "text"));
$recibos = mysql_query($query_recibos, $usersconexion) or die(mysql_error());
$row_recibos = mysql_fetch_assoc($recibos);
$totalRows_recibos = mysql_num_rows($recibos); 



Código PHP:
  $query_recibos=mysql_query("SELECT total FROM tbrecibos ");
while(
$row_recibos=mysql_fetch_assoc($query_recibos))
$total= $total + $row_recibos['total'];

echo 
$total //muestra la suma tota