-2

Not echoing result, I think because result is a sum and not a specific entry in the db?

<?PHP
error_reporting('E_ALL');
include('session.php');
include('config.php');
$sql="SELECT SUM(grading) FROM (SELECT * FROM 'trails' WHERE `name` = 'Free Flow' ORDER BY `id` DESC LIMIT 5) AS DATA";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)) {
echo $row['grading'];
}
?>

Updated version giving lank page...

<?PHP
error_reporting(E_ALL);
include('session.php');
include('config.php');
$sql="SELECT SUM(grading) AS grading FROM (SELECT * FROM `trails` WHERE `name` = "Free Flow" ORDER BY `id` DESC LIMIT 5) AS DATA";
$result=mysql_query($sql);
while($row=mysql_fetch_array($result)) {
echo $row['grading'];
}
?>
  • 5
    `SELECT SUM(grading) as grading` before using in the row index. – Abhik Chakraborty Jul 08 '14 at 11:52
  • Are you sure there's no error being reported here? Check your PHP logs. – David Jul 08 '14 at 11:52
  • Apart from the right solution written from @AbhikChakraborty - ``error_reporting(E_ALL);`` instead of ``error_reporting('E_ALL');``. ``E_ALL`` is a constant. – thedom Jul 08 '14 at 11:54
  • Abhik, please elaborate? David no error is being thrown.. Query gives correct result in phpmyadmin. – user1287814 Jul 08 '14 at 11:57
  • 3
    And you shouldn't use mysql_* functions because they are deprecated. Visit this [link](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) for more information – Giulio De Marco Jul 08 '14 at 12:05
  • Thanks so much guys been struggling with this for hours.. much appreciated, all of you.. :D – user1287814 Jul 08 '14 at 12:11

4 Answers4

1

Identifiers may begin with a digit but unless quoted may not consist solely of digits.

Database, table, and column names cannot end with space characters.

Database and table names cannot contain “/”, “\”, “.”, or characters that are not permitted in file names.

The identifier quote character is the backtick (“`”):

Remove apostrophes after FROM (i mean FROM 'trails') and replace with backticks " ` ".

Your sql query would then look like this:

$sql="SELECT SUM(`grading`) as `grading`
      FROM (SELECT * FROM `trails` WHERE `name` = 'Free Flow'
      ORDER BY `id` DESC LIMIT 5) AS DATA";

Source: Schema Object Names

Community
  • 1
  • 1
Zemistr
  • 1,049
  • 7
  • 10
0

Make sure that you connection is successfully connected, SQL query is correct, database table has entries and you are using correct column name to print like $row['grading'].

Saeed Afzal
  • 368
  • 1
  • 5
  • 18
0

The problem comes from :

error_reporting('E_ALL');

'E_ALL' isn't the constant, it's a string. PHP will silently cast it to int, and make it the configuration value for error reporting.

 (int) 'E_ALL' == 0

error_reporting(0) hide all errors, that's why you got a blank page.

NPlay
  • 108
  • 1
  • 6
0

You have to create an index to SUM, so:

SELECT SUM(grading) AS sum_grading FROM (SELECT * FROM trails WHERE `name` = 'Free Flow' ORDER BY `id` DESC LIMIT 5) AS DATA

And then:

echo $row['sum_grading']
pavel
  • 26,538
  • 10
  • 45
  • 61