0

I have searched all over the internet and have found various "helpful" tips about how to do a column sum with PHP and a MySQL table. The problem is that I can not get ANY of them to work.

Essentially I have a very simple database with 2 users. The table within the database is called users and each entry has a 'Name' and a 'Total Steps'. All I want to do is display the result of the total steps of each user and then a sum of their steps.

Here is my code:

<?php
$steps = mysql_query("SELECT SUM(Total_Steps) AS value_sum FROM users");
$row = mysql_fetch_assoc($steps);
$sum = $row['value_sum'];
?>

However, I get this error upon loading the page:

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /nfs/nfs4/home/msnether/apache/htdocs/st.php

Since I don't know PHP or MySQL very well yet, this is quite frustrating and I would appreciate any help.

Micah
  • 263
  • 1
  • 5
  • 14
  • 3
    You need to establish a mysql-connection (`mysql_connect`) first. But be aware that **you shouldn't use `mysql_*` functions in new code** ([why?](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php)), they are [deprecated](https://wiki.php.net/rfc/mysql_deprecation). Use [PDO or MySQLi](http://php.net/manual/en/mysqlinfo.api.choosing.php) instead – kero Apr 01 '13 at 02:18
  • where your database connection.. – Jhonathan H. Apr 01 '13 at 02:19
  • `SELECT SUM(Total_Steps) AS value_sum FROM users` If that query doesn't work there can be two reasons. `1:` You are not connected to properly selected database, are you? `2:` Those field/table names are different in your database – Hanky Panky Apr 01 '13 at 02:21
  • As @HankyPankyㇱ said, there is a deeper problem than the code you shown here, something is not matching up or the results couldn't be computed. – kabuto178 Apr 01 '13 at 02:23

2 Answers2

0

If you "Do not know PHP and Mysql well" then, knowing or die(mysql_error()) will be a pretty useful tool for you in the future. Just add it here, and see what mysql will make you understand politely.

$steps = mysql_query("SELECT SUM(Total_Steps) AS value_sum FROM users") or die(mysql_error());
0

Heres the basics step for you if your a beginner in using php and mysql..

FIRST : SET UP CONFIGURATION FOR DATABASE USER,PASS,HOST

$conn = mysql_connect('database_server','database_username','database_password');

SECOND : Execute a database connection.

mysql_select_db($conn,'database name');

THIRD : Create a Query(you may insert your query here)..

$sql= mysql_query("SELECT SUM(Total_Steps) AS value_sum FROM users");

FINAL : SHOWN RECORDS USING MYSQL FUNCTIONS LIKE..

while($row = mysql_fetch_array($sql)){
    echo $row['dabatase_columnname'];
    echo $row['database_columnname'];
}
Jhonathan H.
  • 2,734
  • 1
  • 19
  • 28
  • I have set up a connection, and I confirmed it works. I just took that part of the code out because it was irrelevant to my issue. Thank you to those who responded. I will try and see if the above comment works. Thanks! – Micah Apr 01 '13 at 03:54