0

I have a problem with my code.

I don't understand this error:

Notice: Object of class stdClass could not be converted to int in C:\wamp\www\Site\include\fonction_panier.php on line 136

My line 136:

return $total + $total*$_SESSION['panier']['tva']/100;

This line is in this function:

function montantlGlobalTVA() {
    $total = 0;

    for ($i = 0; $i<count($_SESSION['panier']['libelleProduit']); $i++) {
        $total += $_SESSION['panier']['qteProduit'][$i]*$_SESSION['panier']['prixProduit'][$i];
    }

    return $total + $total*$_SESSION['panier']['tva']/100;
}

TVA is in my database:

$select = $db->query("SELECT tva FROM produits");
        $tva = $select->fetch(PDO::FETCH_OBJ);
        $_SESSION['panier']['tva']= $tva;

When I do a var_dump():

<td><?php echo var_dump($_SESSION['panier']['tva'])." %";?></td>

I get this result:

object(stdClass)[1] public 'tva' => string '18' (length=2)

thanksd
  • 54,176
  • 22
  • 157
  • 150
dinamo
  • 31
  • 5
  • It is saying that `$_SESSION['panier']['tva']` is not an integer, that means that you cannot do math with it. Echo it and see what it is. – Evan Carslake Apr 10 '17 at 21:28
  • You can cast using ``(int)`` e.g. ``$total += (int) $_SESSION['panier']['qteProduit'][$i] * (int) $_SESSION['panier']['prixProduit'][$i];`` – alistaircol Apr 10 '17 at 21:33
  • Possible duplicate of [Reference - What does this error mean in PHP?](http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – miken32 Apr 10 '17 at 22:33

2 Answers2

1

I bet you're trying to get the value of the column tva. When fetching with the strategy PDO::FETCH_OBJ, each row is returned as an object.

You can get the value by calling the tva property set in the object being returned by fetch():

$row = $select->fetch(PDO::FETCH_OBJ);
$_SESSION['panier']['tva']= $row->tva;

Instead of returning the row (object) in the session, the actual value retrieved from the database is now being stored in the session.

Dygnus
  • 621
  • 6
  • 14
1
$select = $db->query("SELECT tva FROM produits");
        $tva = $select->fetch(PDO::FETCH_OBJ);
        $_SESSION['panier']['tva']= $tva;

Straight form php.net:

PDO::FETCH_OBJ: returns an anonymous object with property names that correspond to the column names returned in your result set http://php.net/manual/en/pdostatement.fetch.php

Maybe you want to use PDO::FETCH_ASSOC ?

Frank B
  • 3,667
  • 1
  • 16
  • 22
  • When I put PDO::FETCH_ASSOC! I have an other error `Fatal error: Unsupported operand types in C:\wamp\www\Site\include\fonction_panier.php on line 136` I try to remove the division but nothing happen.Do you have an idea? – dinamo Apr 11 '17 at 17:03
  • This : `return $total + $total*$_SESSION['panier']['tva']/100;` – dinamo Apr 12 '17 at 18:02