-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'];
}
?>

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,051
  • 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
  • 357
  • 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
  • 25,361
  • 10
  • 41
  • 58