Question

I am receiving a 'HY000 General error' when running a prepared statement to UPDATE a table row. The query seems to run, and the row is updated successfully. My code and the error returned are detailed below. If anyone can point me in the right direction as to resolving this issue I'd be very grateful.

Code Example:

$query = 'UPDATE users SET active = 1 WHERE email = ? AND activationCode = ?';

$stmt = $this->ds->prepare($query);
$stmt->bindValue(1, $email);
$stmt->bindValue(2, $code);
$stmt->execute();

$row = $stmt->fetch();

Error details:

File: C:\apache2\htdocs\ppi-framework\Vendor\Doctrine\Doctrine\DBAL\Statement.php
Line: 189
Message: SQLSTATE[HY000]: General error
Code: HY000
Was it helpful?

Solution

Yes, fetch() is expecting the result coming from a SELECT, which isn't what your query is performing.

I recommend rather than interacting directly with PDO, through PPI's DataSource component, that you use PPI\DataSource\ActiveQuery component. This performs the relevant PDO code under the hood, which is ofcourse being performed by Doctrine DBAL (which PPI Framework is abstracting).

The following is an example of how to use PPI's ActiveQuery class.

<?php
namespace App\Model;
class User extends \PPI\DataSource\ActiveQuery {

    protected $_meta = array(
        'table'   => 'users',
        'primary' => 'id',
        'conn'    => 'main' // See your connections.php configuration
    );
}

Thats all you need, now PPI can do the rest for you. To update a query you can do:

<?php
$model  = new \App\Model\User();
$data   = array('active' => 1);
$where  = array('email' => 'x', 'activationCode' => 'x');
$result = $model->update($data, $where);

To see what's going on with ActiveQuery->update() you can begin tracing here: https://github.com/ppi/framework/blob/master/PPI/DataSource/PDO/ActiveQuery.php#L127

Good luck.

Paul Dragoonis.

OTHER TIPS

You are executing an update statement, then trying to fetch a row from the resultset (that is empty). That's where you get the exception.

Remove the fetch and you're good to go.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top