1
I’m new to stackoverflow, but that I use the platform to cease my direct doubts, had never made an account. Anyway, I have an atypical problem with a PHP code. I’m making a query to the database (directly from phpmyadmin in xammp), which as you can see in the code below returns exactly what I want:
SELECT sum(cha_valor) as 'valorTotal' FROM chamadas WHERE cha_nome_funcionario = 'José Garcia Pinto'
return = 148.00
This value is the correct one. So knowing the return of the right value let’s go to PHP code.
<?php
$sql = "SELECT * FROM user_employee";
$query = mysqli_query($conn, $sql);
?>
data.addRows([
<?php
while($dados = mysqli_fetch_assoc($query)){
?>
['<?php echo $dados['user_nome'] ?>',
<?php $sql = "SELECT sum(cha_valor) FROM chamadas WHERE cha_nome_funcionario = '" . $dados['user_nome'] . "'";
$query = mysqli_query($conn, $sql);
$result = mysqli_fetch_assoc($query);
echo $result[0];?>],
<?php }; ?>
I’m using a graphic creation API, GOOGLECHARTS. I’m making a chart that shows two information, the user’s name and, where is giving the problem, the amount of money he earned (which is stored in the bank). The error occurs because I can not print the result on the screen (one of them was to be 148 as I showed the return to you), when using this echo $result[0], it gives an error that the variable is empty or there is no value for this index. If I try to use string index using simple quotes('cha_call') it gives the same error. If I try to use only $result, it gives the notice-array-to-string-Conversion error. Anyway, I don’t know what else to do! When I put $result in var_dump(), this appears:
array(1) { ["sum(cha_valor)"]=> string(6) "148.00" }
That is, I assume that the value is returning correctly, as in Phpmyadmin itself, but I cannot display the value to use in the graph. The Project is in my github and it is very easy to make preparations and start coding. If you want to go in to see the whole problem code, just go in to this github link https://github.com/FeCesar/Order_of_service_system and enter the directory (admin/index.php) of the problem that will be commented at the very beginning of the page. I hope someone can give me a light! Thanks from now on!
change
$result = mysqli_fetch_assoc($query);
for$result = mysqli_fetch_row($query);
– Augusto Vasques