1

I can't figure this out. This is my variables:

$id = 1; 
$exp = 1;
$energy = 1;
$payout = rand ( 100 , 300 );

here is the query

    $sql =  'update players set ' .
            'exp = exp + :exp, ' .
            'energy = energy - :energy, ' .
            'cash = cash + :cash ' .
            'where id = :id';

An example of this as a string would be:

update players set exp = exp + 1, energy = energy - 1, cash = cash + 170 where id = '1'

When I input this in phpmyadmin it works. Thats why I don't understand why this does not work:

    try
    {
        $stmt = $this->db->prepare($sql);

        $stmt->bindParam(':id', $id, PDO::PARAM_INT);
        $stmt->bindParam(':exp', $exp, PDO::PARAM_INT);
        $stmt->bindParam(':energy', $energy, PDO::PARAM_INT);
        $stmt->bindParam(':cash', $payout, PDO::PARAM_INT);

        $result = $stmt->execute();

        $stmt->closeCursor();
    }
    catch (Exception $e)
    {
        die ($e->getMessage() );
    }

    if ( !$result )
    {
        trigger_error("mysql: " . $sql, E_USER_ERROR);
        $data = array( "message" => T_gettext("Something went wrong."));
        return json_encode( $data );
    }

This results in a false $result = something wrong with query.

What have I done wrong? + how do I get a more detailed mysql error message from PDO?

ganjan
  • 7,356
  • 24
  • 82
  • 133

1 Answers1

0

Are you actually throwing exceptions from your connection for you to catch? Try this and see if you get any error messages:

$db = new PDO($connection_string);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
Nev Stokes
  • 9,051
  • 5
  • 42
  • 44