Reputation: 11
$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?
Upvotes: 1
Views: 139
Reputation: 708
$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?
Upvotes: 0
Reputation: 361
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]
Upvotes: 0
Reputation: 26732
Use this or var_dump() to know what ur getting in $veldtotal
echo "<pre>";
print_r($veldtotal);
Upvotes: 0
Reputation: 6992
That would be because mysql_fetch_array
returns an array. You can get to the result using $veldtotal[0]
.
Upvotes: 2