0

I am trying to query an SQL table, and then have all the values from one column (it's a "tinyint(1)") added up and then printed, using array_sum. (Currently there are only two rows).

When both rows have a "1" in that column, it gives 2, the correct answer. And when they are both "0" it gives 0, correct again. But when one is "1" and one "0" it gives 2, not 1.

This is my code

$con = mysql_connect("XXX","XXX","XXX");

if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("XXX_test", $con);

$day = $_GET["day"];

$query = mysql_query("SELECT $day FROM sites");

if (!$query)
  {
  die('Could not query: ' . mysql_error());
  }

$rows = mysql_fetch_array($query);

echo array_sum($rows);

mysql_close($con);

?>

Thanks for any help.

--UPDATE--
I just solved this myself.

With this, if you want to know: $query = mysql_query("SELECT name, SUM($day) AS Alerts FROM sites");

while($row = mysql_fetch_array($query))
  {
  echo $row['Alerts'];
  echo "<br />";
  }
1
  • Based on your code nothing is insinuating that $rows is an array. If you were $rows[] = mysql_fetch_result($query,0); then summed it I would wonder why it's not working. But you're getting a key-value array back, so what are you expecting to sum? Commented Dec 19, 2011 at 15:34

5 Answers 5

4

This in fact never works correctly.

$rows = mysql_fetch_array($query);

That line only ever fetches the first row. The second row is never retrieved.

The reason it gives 2 using array_sum is that mysql_fetch_array gets the result in two different formats, with keys both by name and number. So you'll get something like this:

array(
    0 => '1',
    'Tuesday' => '1'
)

Obviously this will always be 2 or 0.

You should almost certainly do a count on the DB server:

SELECT COUNT(*) FROM sites WHERE $day=1

Note, however, that your biggest current problem is the enormous gaping SQL injection hole. Sanitise your input.

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

6 Comments

Thanks, but what do you mean by the last two sentences?
@ACarter Read up on SQL injection. Basically, with your code as it currently is, any malicious user could execute arbitrary SQL on your database.
Oh, yes. When it is finished, I am planning to add a check, so the SQL connection doesn't even start unless a POST parameter is the same as a 'password' given in the code. But thanks for saying that.
So if the new code is: $query = mysql_query("SELECT COUNT(*) FROM sites WHERE $day=1"); How do I print it? Thanks
No, that won't necessarily help. In this case, you should probably check that $day is one of the possible days of the week. I'm not sure your database schema (days of the week as column names?) is well designed, but nonetheless... You should be able to do $row = mysql_fetch_row($query); $num = $row[0];
|
3

mysql_fetch_array() only fetches one row at a time. You need to fetch the rows in a while loop. Then you can either append the results to an array and sum those, or sum them in the loop. You can also SUM in mysql anyway, so you might look into using that.

I'm also obligated to suggest that you use PDO or some other DB wrapper instead of mysql_. You also need to escape variables you use in queries under most circumstances, especially if they come from _GET.

Comments

2

I would suggest doing it in the mysql query:

SELECT SUM($day) FROM sites

also, please escape the $day variable.

2 Comments

Thanks. What do you mean by "escape the variable"?
$day = mysql_real_escape_string($_GET['day'], $con); doing this, you prevent sql injection attacks. right now, you just pass the gat-variable without protecting it from bad input
1

More of a comment:

array_sum can not work with the array you're passing to it. You need to pass an array for what the function actually works, like array(1, 2, 3) which will give you 6 then.

Next to that, let MySQL itself calculate the SUM (or COUNT?), so it's easier to handle in your script as others have posted.

Comments

0

I just solved this myself.

With this, if you want to know: $query = mysql_query("SELECT name, SUM($day) AS Alerts FROM sites");

while($row = mysql_fetch_array($query))
  {
  echo $row['Alerts'];
  echo "<br />";
  }

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.