15

I am getting this error:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General error' in ...

..whenever I execute this code with PDO:

//Select data from the topic.
$s = $dbh->prepare("SELECT * FROM forum_topics WHERE forum_id=:forum_cat_id AND topic_id=:topicid");
$s->bindParam(':forum_cat_id', $forum_cat_id);
$s->bindParam(':topicid', $topicid);
$s->execute();
$f = $s->fetch();

$s = $dbh->prepare("UPDATE forum_cats 
    SET 
        forum_last_postid = :last_post_id, forum_last_posttime = :time, 
        forum_last_userid = :userid, forum_last_username = :username, 
        forum_posts=forum_posts+1 
    WHERE forum_id = :forum_cat_id");
$s->bindParam(':last_post_id', $last_post_id);
$s->bindParam(':time', $time);
$s->bindParam(':userid', $userid);
$s->bindParam(':username', $userdata['username']);
$s->bindParam(':forum_cat_id', $forum_cat_id);
try {
    $s->execute();
}
catch(PDOException $e) {
    die($e->getMessage());
}

if (count($s->fetchAll()) == 0) {
    return 3;
}

I have no idea why this is happening. I've checked the query, and I simply cant find any errors..

gam6itko
  • 15,128
  • 2
  • 19
  • 18
oliverbj
  • 5,771
  • 27
  • 83
  • 178
  • 1
    I thought `try{}catch()` should be made from the beginning of the query – samayo Dec 01 '13 at 18:58
  • $userdata['username'] Are you sure this is legit? – Mihai Dec 01 '13 at 19:03
  • Call `$f = $s->fetchAll();` instead of `fetch()`. Perhaps there are multiple rows returned and you have not fetched them all before preparing another statement. That can be a source of cryptic errors in MySQL. – Michael Berkowski Dec 01 '13 at 19:04
  • 2
    Wait, and what's the purpose of `count($s->fetchAll())`? At that point, `$s` is the prepared `UPDATE` query, not the `SELECT` query. – Michael Berkowski Dec 01 '13 at 19:06

2 Answers2

36

This is what happens:

  • You are trying to fetch an UPDATE query. You can't do that because UPDATE queries does not return values. If you wish to know how many rows were affected by the query, use the rowCount() function instead. Notice that not all DB Drivers provide the affected rows.

  • You are using undeclared variables (at least in the code you posted here). This isn't the reason for this particular error, but could generate others.

  • You're not using the data you have selected from the database

    Also, it is recommended to make all PDO operations within the try block, otherwise you may get unhandled exceptions.

Community
  • 1
  • 1
Byte Bit
  • 513
  • 5
  • 12
15

For others who came here trying to decipher this esoteric error message as I did, let me add that:

Trying to run fetches on pdo statements like:

$statement->fetchAll();

or

$statement->fetchAll(PDO::FETCH_ASSOC);

...after an INSERT or UPDATE** statement can cause this error (since there is no data to fetch).

**The UPDATE ... RETURNING ... statement is an exception to this.

Kzqai
  • 22,588
  • 25
  • 105
  • 137
  • 2
    What is the proper method to execute an update query then? My update query is built and stored in variable then executed afterwards like this: $results = $readConnection->fetchAll($query); – sparecycle Sep 26 '14 at 18:23
  • @deeperDATA the point is that you should delay inserts/updates/deletes until you are done fetching your result. So instead of `select`, `update`, `fetchAll`, you do `select`, `store updates in temporary variable`, `fetchAll`, `update from temp var` – Mahn Jan 22 '15 at 14:24
  • This has nothing to do with "where/when" you try to fetch data from an update or insert. Updates and inserts simply don't have fetch data to provide! They do *act* upon a certain number of rows, so the metadata of `$statement->rowCount()` *is* available, but no fetch data like select has... – Kzqai Jan 27 '15 at 18:17
  • I had this error also when doing multiple queries in single statement like "USE DBNAME; SELECT..." – Łukasz Gawron Mar 24 '19 at 16:34
  • I had this error when I had `$stmt = $pdo->query("INSERT INTO........` followed by `$stmt->execute(['.......` Obviously I had keyed 'query' not 'prepare' by mistake – – Roy Grubb Dec 06 '21 at 10:13