note 75070 modified in pdo.exec by danbrown

From: Date: Fri, 28 Nov 2008 22:48:04 +0000
Subject: note 75070 modified in pdo.exec by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-147383@lists.php.net to get a copy of this message
If you do this: <?php $res = $dbh->query("SELECT * FROM sessions WHERE session_id = '$p_sessID'"); $l_records = $res->fetch(PDO::FETCH_ASSOC); if( $l_records ) { // ...update session-data $l_theQuery = "UPDATE sessions SET session_expires='$newExp', session_data='$p_sessData' WHERE session_id='$p_sessID'"; echo $l_theQuery; $l_stmt = $this->db->prepare($l_theQuery); if ( $l_stmt ) { $l_rows = $l_stmt->execute(); } } ?> You will get nothing. But do this: <?php $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); ?> Prior to the code above, you will get this: "PDO::prepare(): SQLSTATE[HY000]: General error: 2014 Cannot execute queries while other unbuffered queries are active. Consider using PDOStatement::fetchAll(). Alternatively, if your code is only ever going to run against mysql, you may enable query buffering by setting the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY attribute." So, instead of fetch(), use fetchAll(), it will make you less insane. Incidentally, the INSERT statement that I was issuing, if the record that I needed to update didn't yet exist, after the initial fetch() command worked perfectly. Changing to fetchAll() fixed it. --was-- If you do this: $res = $dbh->query("SELECT * FROM sessions WHERE session_id = '$p_sessID'"); $l_records = $res->fetch(PDO::FETCH_ASSOC); if( $l_records ) { // ...update session-data $l_theQuery = "UPDATE sessions SET session_expires='$newExp', session_data='$p_sessData' WHERE session_id='$p_sessID'"; echo $l_theQuery; $l_stmt = $this->db->prepare($l_theQuery); if ( $l_stmt ) { $l_rows = $l_stmt->execute(); } } You will get nothing. But do this: $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); Prior to the code above, you will get this: "PDO::prepare(): SQLSTATE[HY000]: General error: 2014 Cannot execute queries while other unbuffered queries are active. Consider using PDOStatement::fetchAll(). Alternatively, if your code is only ever going to run against mysql, you may enable query buffering by setting the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY attribute." So, instead of fetch(), use fetchAll(), it will make you less insane. Incidentally, the INSERT statement that I was issuing, if the record that I needed to update didn't yet exist, after the initial fetch() command worked perfectly. Changing to fetchAll() fixed it. http://php.net/manual/en/pdo.exec.php

« previous php.notes (#147383) next »