ClanCats/Hydrahon

Documentation for handling return values from UPDATE etc.

Closed this issue · 3 comments

Can anyone point me at how to check the result from an UPDATE

Depends fully on your implementation, you could do something like this to return a success value:

$connection = new PDO('mysql:host=localhost;dbname=my_database', 'username', 'password');

// create a new mysql query builder
$h = new \ClanCats\Hydrahon\Builder('mysql', function($query, $queryString, $queryParameters) use($connection)
{
    $statement = $connection->prepare($queryString);
    $success = $statement->execute($queryParameters);

    // when the query is fetchable return all results and let hydrahon do the rest
    if ($query instanceof \ClanCats\Hydrahon\Query\Sql\FetchableInterface)
    {
        return $statement->fetchAll(\PDO::FETCH_ASSOC);
    }
    
    return $success;
});

Or you can simply return the statement and check the row count:

$connection = new PDO('mysql:host=localhost;dbname=my_database', 'username', 'password');

// create a new mysql query builder
$h = new \ClanCats\Hydrahon\Builder('mysql', function($query, $queryString, $queryParameters) use($connection)
{
    $statement = $connection->prepare($queryString);
    $statement->execute($queryParameters);

    // when the query is fetchable return all results and let hydrahon do the rest
    if ($query instanceof \ClanCats\Hydrahon\Query\Sql\FetchableInterface)
    {
        return $statement->fetchAll(\PDO::FETCH_ASSOC);
    }

    return $statement;
});

$statement = $h->table('foo')->update(['text' => 'bar'])->execute();
echo $statement->rowCount();

*Disclaimer, I did not test these examples

I'm doing the latter but I'm getting $statement as NULL whether the call succeeds or fails. I know it succeeds because the database was updated.

ok. i was missing the return $statement; so because it wasn't fetchable mean it didn't return the output
thanks for your help @mario-deluna