Home > Mobile >  How can I solve Object of class stdClass could not be converted to string?
How can I solve Object of class stdClass could not be converted to string?

Time:03-03

I am facing a major issue on my php project using JQuery datatables. I ma trying to get data from my database but I have the following error : Recoverable fatal error: Object of class stdClass could not be converted to string on line 47". This is my line 47 : <th scope=\"row\"> $client -> ID_client </th> (you can see all the code under this). Do you have any idea of what is causing the problem or how I can fix this ?

[![Error][1]][1]

This is my JS code:

getClients();
function getClients(){
    $.ajax({
        url: 'process.php',
        type: 'post',
        data : {action : 'fetch'}, //on renvoie du json
        success: function(response){
            console.log(response);
            $('#orderTable').html(response);
            $('table').DataTable();
        }
    })
}

This is my php code:

//Récupérer tous les clients
    //on créé une variable pour stocker l'affichage
    $output = '';
    //on vérifie que l'on a au moins un client
    if ($db->countClients() > 0){
        $clients = $db->read();
        //entête du tableau
        $output .= '
        <table >
            <thead>
                <tr>
                <th scope="col">Id</th>
                <th scope="col">Nom</th>
                <th scope="col">Prénom</th>
                <th scope="col">Email</th>
                <th scope="col">Type client</th>
                <th scope="col">Numéro de téléphone</th>
                <th scope="col">Adresse</th>
                <th scope="col">Ville</th>
                <th scope="col">Pays</th>
                <th scope="col">Nombre de réservation</th>
                <th scope="col">Actions</th>
                </tr>
            </thead>
            <tbody>
        ';```

            foreach($clients as $client){
                $output .= "
                <tr>
                            <th scope=\"row\"> $client -> ID_client </th>
                            <td>$client -> Nom </td>
                            <td>$client -> Prenom </td>
                            <td>$client -> Email </td>
                            <td>$client -> Type_client</td>
                            <td>$client -> Num_tel </td>
                            <td>$client -> Adresse </td>
                            <td>$client -> Ville </td>
                            <td>$client -> Pays </td>
                            <td>$client -> Nb_reservation </td>
                            <td>
                            <!--Bouton détails-->
                            <a href=\"\" class=\"text-info me-2 infoBtn\" title=\"Voir détails\">
                                <i class=\"fas fa-info-circle\"></i></a>
                            <!--Bouton modifier-->
                            <a href=\"\" class=\"text-primary me-2 editBtn\" title=\"Modifier\">
                                <i class=\"fas fa-edit\"></i></a>   
                            <!--Bouton supprimer-->    
                            <a href=\"\" class=\"text-danger me-2 deleteBtn\" title=\"Supprimer\">
                                <i class=\"fas fa-trash-alt\"></i></a>        
                            </td>
                </tr> ";
            }
        $output .= "</tbody> </table>";
        echo $output;
        //print_r($output);

    } else {
        echo "<h2> Aucun client dans la base de données</h2>";
    }

      public function read(){
        //fetchAll() permet de récupérer d'un coup l'ensemble du résultat d'une requête
        return $this->getconnexion()->query("SELECT * FROM table_client ORDER BY ID_client")->fetchAll(PDO::FETCH_OBJ);
    }

    //fonction qui compte combien de clients sont présents dans la table
    public function countClients(): int{
        // [0] sera ce qui contiendra le count
        return (int)$this->getconnexion()->query("SELECT COUNT(ID_client) as count FROM table_client")->fetch()[0];
    }


  [1]: https://i.stack.imgur.com/Uys7M.png
  [2]: https://i.stack.imgur.com/pLoEL.png

CodePudding user response:

This is a solution that I found:

<th scope=\"row\"> {$client->ID_client} </th>
                        <td> {$client -> Nom} </td>
                        <td>{$client -> Prenom} </td>
                        <td>{$client -> Email} </td>
                        <td>{$client -> Type_client}</td>
                        <td>{$client -> Num_tel} </td>
                        <td>{$client -> Adresse} </td>
                        <td>{$client -> Ville} </td>
                        <td>{$client -> Pays} </td>
                        <td>{$client -> Nb_reservation} </td>

I just now saw that I was expecting to receive a JSON type but was trying to show a string !! I added {} and it worked perfectly

  • Related