Jump to content

Counting inside rows


sazuka

Recommended Posts

Good eveing everyone , I got actually a simple problem but im really still new into PHP.

Hope someone can find a solution to it..

 

I have a mysql table and I want to sum up all numbers inside the field Counts..

This is my table:  -> example here the output would be 56..

 

ID    Counts

1          5

2          5

3          10

4          36

Link to comment
https://forums.phpfreaks.com/topic/190448-counting-inside-rows/
Share on other sites

Actually I have everything I believe Premiso I tried to make a new .php file and

added this:

No error and nothing shows up

<?php
session_start();
$con = mysql_connect("localhost", "dusername","passpass");
if(!$con){
echo('could not connect'.mysql_error());
}
else{
mysql_select_db("Models",$con);
?>


  <?php  

$access = "Select SUM(Wins) From Models";
if(mysql_query($access)){
	$resultaccess = mysql_query($access);
		echo('$resultaccess');
	}
?>


  <?php
  	}

  ?>

 

 

Two problems:

 

   if(mysql_query($access)){
      $resultaccess = mysql_query($access);
         echo('$resultaccess');
      }

 

First, you are doing the query twice. Second, you are echoing a variable inside of Singlequotes, which takes $ literally. Revised version:

 

   if($resultaccess = mysql_query($access)){
         $count = mysql_result($resultaccess, 0, 0);
         echo "Returned: " . $count;
      }

 

As an offtopic note, you really do not need to go in and out of PHP like you do, stay in PHP while you are processing PHP. No need to go in and out.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.