Error: Object of class mysqli_result could not be converted to string



  • I import from my index a classticket file where I have a sql query that aims to add the values of each ticket generated by the field priority of a database.

    Archive index.php

    $ganancia = $ticket->CountTicketCloseEmpleado($conexion, $_SESSION['ticket_id']);
    

    Archive classticket.php

    function CountTicketCloseEmpleado($conexion, $id){
    
    $ganancia = mysqli_query($conexion, "SELECT SUM(prioridad)
                                         FROM ticket WHERE status = '3' AND id_usuario = ".$id." ") 
                                         or die("Error listando Ticket: ".mysqli_error($conexion));
    
    return $ganancia;
    

    }

    By making the value of $ganancia
    I got the next mistake.

    Recoverable fatal error:
    Object of class mysqli_result could not be converted to string



  • The sql query command mysqli_query returns a "object", therefore is assigned to $ganacia , when making echo $ganancia you want to print $ganancia, which is not string and there is born Recoverable fatal error: Object of class mysqli_result could not be converted to string. since $ganance is not string.

    If you want to print all of the results of the query try with var_dump($ganancia)

    Technically, mysqli_query returns a type object mysqli_result

    If we want to print the result of the sum of your query could be something like this using mysqli_fetch_assoc

    function CountTicketCloseEmpleado($conexion, $id){
    
    if ($resultado =mysqli_query($conexión, "SELECT SUM(prioridad) as suma FROM ticket WHERE status = '3' AND id_usuario = ".$id." ")) 
    {
        $row= mysqli_fetch_assoc($resultado);
    
        echo $row['suma'];
    

    #no olvidar hacer return

    }
    

    }


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2