title: Calculate Pecent Value from Sum of Two Columns in MySQL Database
body: <p>I am attempting to get total values from two columns (I have already done this) -- and take these values and create a percent from them. In this case I have a Weighted Value and a Total Value. I need to know what Percent of the Total Value my Weighted Value is.</p>
  
  <p>I have tried:</p>
  
  <pre><code>    $result = mysqli_query($mysqli, "SELECT SUM(WEIGHTEDV / VALUE) FROM test.service");
  
      $result = mysqli_query($mysqli, "SELECT WEIGHTEDV, VALUE, (WEIGHTEDV / VALUE) AS percent FROM test.service;
  
      $result = mysqli_query($mysqli,"SELECT (WEIGHTEDV / VALUE)*10 AS Percent
  FROM test.service");
  </code></pre>
  
  <p>There were a few other attempts I do not readily remember.</p>
  
  <p>Here is more complete code:</p>
  
  <p>Login &amp; Working SUM of VALUE</p>
  
  <pre><code>
      &lt;?php
                      //LOGIN INFO
                      $db_host = 'localhost';
                      $db_user = 'root';
                      $db_pw = "********";
                      $database = "test";
                      //LOGIN VARIABLE
                      $mysqli = new mysqli($db_host, $db_user, $db_pw);
                      //VERIFY LOGIN INTEGRITY
                      if ($mysqli === false) {
                          die("Cannot connect to Database Server" . $mysqli-&gt;connect_error);
                      }
                      if (!mysqli_select_db($mysqli, $database)) {
                          die("Cannot select test Database" . $mysqli-&gt;connect_error);
                      }
                      $result = mysqli_query($mysqli, "SELECT SUM(VALUE) FROM test.service");
                      //PRINT ROWS
                      $fields_num = mysqli_num_fields($result);
                      while($row = mysqli_fetch_row($result)) {
                          foreach($row as $cell) {
                              echo "$$cell";
                          }
                      }
                      ?&gt;
  
  </code></pre>
  
  <p>Working SUM of WEIGHTEDV</p>
  
  <pre><code>
                          &lt;?php
                      $result = mysqli_query($mysqli, "SELECT SUM(WEIGHTEDV) FROM test.service");
                      //PRINT ROWS
                      $fields_num = mysqli_num_fields($result);
                      while($row = mysqli_fetch_row($result)) {
                          foreach($row as $cell) {
                              echo "$$cell";
                          }
                      }
                      ?&gt;
  </code></pre>
  
  <p>The expected output with the current totals in my Table is </p>
  
  <pre><code>Expected Output: %54.8
  </code></pre>
  
  <p>Test Variables:</p>
  
  <pre><code>VALUE: 38940.00
  WEIGHTEDV:21345.00
  </code></pre>
  
  <p>Both have a type of (I changed the type to INT -- same result):</p>
  
  <pre><code>DECIMAL(12,2)
  </code></pre>
  
  <p>Example of actual output (directly out of the HTML table): </p>
  
  <pre><code>65.000000100.0000000.00000050.00000025.000000100.00000050.000000
  </code></pre>
  
  <p>Current code with above output:</p>
  
  <pre><code>    $result = mysqli_query($mysqli,"SELECT (WEIGHTEDV / VALUE)*100 AS Percent
  FROM test.service");
  </code></pre>
  
  <p>This is boggling me pretty good. I am sure it is something simple I am overlooking. Any assistance would be greatly appreciated as to why I cannot get a simple % value output.</p>