0

For some reason, this custom PDO class fails to write to the database. It simply quietly fails - no error message thrown. A very similar custom PDO class (ReadPDO) works wonderfully for reading from the database. The SQL statement generated works fine when it's queried to the DB through PHPMyAdmin. I've double-checked the user permissions, and everything seems in order.

I suspect I'm misunderstanding how something works. Any ideas?

// Creates a write-only PDO, using config settings from inc_default.php 
class WritePDO extends PDO{

    public function __construct(){
        //Pull global DB settings
        global $db;
        global $write_host;
        global $write_username;
        global $write_password;

        try{
            parent::__construct("mysql:dbname={$db};host={$write_host}", $write_username, $write_password);
        } catch (PDOException $e){
            echo 'Connection failed: ' . $e->getMessage();
        }
    }
}

private function updatePlayer(){
    $conn = new WritePDO();
    $sql = "UPDATE {$this->hvz_db}
        SET 
        hvz_bitten      ='{$this->hvz_bitten}',
        hvz_died        ='{$this->hvz_died}',
        hvz_feedCode    ='{$this->hvz_feedCode}',
        hvz_status      ='{$this->hvz_status}',
        hvz_feeds       ='{$this->hvz_feeds}',
        hvz_lastFed     ='{$this->hvz_lastFed}',
        hvz_ozOpt       ='{$this->hvz_ozOpt}',
        hvz_parent      ='{$this->hvz_parent}'
        WHERE users_id  ={$this->id}";
    $query = $conn->exec($sql);
}

The SQL it spits out is as follows:

UPDATE hvz_2011_spring SET hvz_bitten ='', hvz_died ='', hvz_feedCode ='NOMNOM', hvz_status ='Human', hvz_feeds ='0', hvz_lastFed ='', hvz_ozOpt ='0', hvz_parent ='' WHERE users_id =1
Brian Bauman
  • 668
  • 5
  • 22
  • possible duplicate of [How to squeeze error message out of PDO?](http://stackoverflow.com/questions/3726505/how-to-squeeze-error-message-out-of-pdo) – Pekka Feb 23 '11 at 21:58
  • It's not quite a duplicate- though being able to squeeze an error message out of the PDO would help in trouble-shooting this particular problem. – Brian Bauman Feb 23 '11 at 22:09

1 Answers1

1

are you sure the sql is correct?

The exec doesn't send any error message.

Try doing var_dump($conn->errorInfo()); after $conn->exec($sql);

/Emil

Bahamot
  • 26
  • 3
  • The error it throws is: array(3) { [0]=> string(5) "42000" [1]=> int(1143) [2]=> string(102) "SELECT command denied to user 'refactor2'@'localhost' for column 'users_id' in table 'hvz_2011_spring'" } Let me try something... – Brian Bauman Feb 23 '11 at 22:41
  • That did it - apparently it needs access to the SELECT command to perform an UPDATE command. Who knew? – Brian Bauman Feb 23 '11 at 22:43