Problems with displaying data from an array

Asked

Viewed 57 times

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!

1 answer

1


The mysqli_fetch_assoc creates an array where the key is the column name. Your queries are different, at first use as 'valorTotal', no longer in PHP.


Assuming you use the as 'valorTotal':

$sql = "SELECT sum(cha_valor) as `valorTotal` FROM chamadas WHERE cha_nome_funcionario = '" . $dados['user_nome'] . "'"; 

Just change the $result[0] for $result['valorTotal'].


        $sql = "SELECT sum(cha_valor) as `valorTotal` FROM chamadas WHERE cha_nome_funcionario = '" . $dados['user_nome'] . "'"; 
        $query = mysqli_query($conn, $sql); 
        $result = mysqli_fetch_assoc($query); 
        echo $result['valorTotal'];

If you prefer to access the values using $result[0], you should use the mysqli_fetch_row().

  • Wow, thank you so much! I tried everything, did not imagine that it would be so simple to solve... Strange that in the end, even bringing the information with the mysqli_fetch_row(), and the information being of the whole type, I had to convert to whole when I printed it on the screen. Finally it was like this the code. <?php $sqlDinheiro = "SELECT sum(cha_valor) FROM chamadas WHERE cha_nome_funcionario = '" . $dados['user_nome'] . "'"; $queryDinheiro = mysqli_query($conn, $sqlDinheiro); $dinheiro = mysqli_fetch_row($queryDinheiro); echo (int)$dinheiro[0];

Browser other questions tagged

You are not signed in. Login or sign up in order to post.