1
$query = "SELECT SUM(Veldspar) FROM hauled WHERE miningrun=2 AND hauler=1";
$result = mysql_query($query) or die(mysql_error());
$veldtotal = mysql_fetch_array($result);

printf("Results:  %s<br>", $veldtotal);
printf("Length of array: %s<br>", count($veldtotal));
printf("Array into Int: %s<br>", (int)$veldtotal);

Why does the first printf return a blank variable?
All I want to be able to do is to get the sum of the query, and pass it to a variable to be displayed on the screen. Can anyone help with this?

2
  • if this code is new, please avoid mysql_* functions, prefer PDO and mysqli features. Commented Dec 19, 2012 at 18:05
  • The rest of the project is not new. I am adding a new feature to an existing opensource project, and I cannot use other things than what is there in the rest of the project. Commented Dec 19, 2012 at 18:35

4 Answers 4

2

That would be because mysql_fetch_array returns an array. You can get to the result using $veldtotal[0].

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

1 Comment

I edited the code as you said, and the return I received when running was the following:<br> Results:
0

Use this or var_dump() to know what ur getting in $veldtotal

echo "<pre>";
print_r($veldtotal);

2 Comments

but wouldn't you get a single value from the SUM() in SQL?, I mean the statement, as it is written will only come up with a single row with the SUM of veldspar
I only want a single row answer as after I pull the value, I will have to do math on it and then store it in a different array.
0

If you want to access the calculated sum in your query, you should name it:

$query = "SELECT SUM(Veldspar) AS total FROM hauled WHERE miningrun = 2 AND hauler = 1";

And read it:

printf("Results:  %s<br>", $veldtotal['total']);
printf("Length of array: %s<br>", count($veldtotal));
printf("Array into Int: %s<br>", (int)$veldtotal);

You can also read it by using $veldtotal[0]

2 Comments

Tried this solution, and it still returned a blank answer. When I add the debug condition in as another user suggested the array had no value in it, but when I do the query on the sql server it returns a value.
Try reading the values in $veldtotal by using <pre><?php print_r($veldtotal); ?></pre> after the query. If the sum is shown, then try changing your printf-statement by an echo.
0
$query = "SELECT SUM(Veldspar) FROM hauled WHERE miningrun=2 AND hauler=1";

$veldtotal = mysql_result(mysql_query($query), 0, 0);

printf("Results:  %s<br>", $veldtotal);
printf("Length of array: %s<br>", count($veldtotal));
printf("Array into Int: %s<br>", (int)$veldtotal);

Besides, what does Length of array mean?

1 Comment

I wanted to make sure I was getting an array of some length.

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.