0

I am trying to get the SUM of a column SnappersScore from a table LastResult. However, there is nothing in the output at all. Not even an error. Any ideas about what I am doing wrong?

Here it is:

<?php

mysql_connect(xxxxxx);
if (mysql_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysql_connect_error();
  }

$result = mysql_query('SELECT SUM(SnappersScore) AS TotalGoals FROM LastResult');
$row = mysql_fetch_assoc($result);
$sum = $row['TotalGoals'];

echo $sum;

?>
8
  • 5
    Have you tried checking what $result contains after running the query? If it is false, an SQL error occured. Commented Oct 11, 2013 at 15:36
  • There is no more support for mysql_* functions, they are officially deprecated, no longer maintained and will be removed in the future. You should update your code with PDO or MySQLi to ensure the functionality of your project in the future. Commented Oct 11, 2013 at 15:36
  • 1
    have you run the query against database and see the result? is it giving you result you want? Commented Oct 11, 2013 at 15:36
  • I have run the query in the database and got the intended result. Commented Oct 11, 2013 at 15:40
  • TotalGoals = 0? Which when echo'd out becomes the string "" ... so you'll see nothing? Commented Oct 11, 2013 at 15:40

1 Answer 1

1

Only you can debug this, see what the error is:

$result = mysql_query('SELECT SUM(SnappersScore) AS TotalGoals FROM LastResult') or die(mysql_error());

I'll save you the repetitive "dont use mysql_*, don't use die in production bla bla bla" - this should point you in the right direction.

This is of course, assuming that when running SELECT SUM(SnappersScore) AS TotalGoals FROM LastResult in SQL works as expected.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.