Do you need to call PDOStatement::closeCursor when you’re done with the statement?

The answer is no, so long as you’re not preparing to execute the statement again. I figured this out by looking at the code for PDOStatement::closeCursor and the MySQL implementation. Seems to me that all the freeing necessary is done in the destructor so if you’re not planning to use the statement again it seems to me that you can safely omit the call to PDOStatement::closeCursor(). On the other hand if you are going to reuse the statement calling closeCursor seems like it’s a pretty important thing to do. It would be nice if PDOStatement::fetchAll() called closeCursor() for us, but I don’t think it does.

PDO: Execute a prepared statement using array for IN clause

See Example #5 here.

/* Execute a prepared statement using an array of values for an IN clause */
$params = array(1, 21, 63, 171);
/* Create a string for the parameter placeholders filled to the number of params */
$place_holders = implode(',', array_fill(0, count($params), '?'));

/*
    This prepares the statement with enough unnamed placeholders for every value
    in our $params array. The values of the $params array are then bound to the
    placeholders in the prepared statement when the statement is executed.
    This is not the same thing as using PDOStatement::bindParam() since this
    requires a reference to the variable. PDOStatement::execute() only binds
    by value instead.
*/
$sth = $dbh->prepare("SELECT id, name FROM contacts WHERE id IN ($place_holders)");
$sth->execute($params);

PHP PDO MySQL Blob

Read PHP MySQL BLOB today to learn how to work with BLOBs. Basically:

public function insertBlob($filePath,$mime){
    $blob = fopen($filePath,'rb');
 
    $sql = "INSERT INTO files(mime,data) VALUES(:mime,:data)";
    $stmt = $this->conn->prepare($sql);
 
    $stmt->bindParam(':mime',$mime);
    $stmt->bindParam(':data',$blob,PDO::PARAM_LOB);
 
    return $stmt->execute();
}

function updateBlob($id,$filePath,$mime) {
 
    $blob = fopen($filePath,'rb');
 
    $sql = "UPDATE files
            SET mime = :mime,
            data = :data
            WHERE id = :id";
 
    $stmt = $this->conn->prepare($sql);
 
    $stmt->bindParam(':mime',$mime);
    $stmt->bindParam(':data',$blob,PDO::PARAM_LOB);
    $stmt->bindParam(':id',$id);
 
    return $stmt->execute();
 
}

public function selectBlob($id) {
 
    $sql = "SELECT mime,
            data
        FROM files
        WHERE id = :id";
 
    $stmt = $this->conn->prepare($sql);
    $stmt->execute(array(":id" => $id));
    $stmt->bindColumn(1, $mime);
    $stmt->bindColumn(2, $data, PDO::PARAM_LOB);
 
    $stmt->fetch(PDO::FETCH_BOUND);
 
    return array("mime" => $mime,
             "data" => $data);
 
}

PDO Persistent Connection in PHP left broken if connected to dropped database

So you use PDO and specify PDO::ATTR_PERSISTENT => true. Then you drop your database. Then you open a new persistent connection and bang! Not working. The trick is to not use persistent connections to databases that may be dropped. And probably an ‘apache2ctl graceful’ after you drop a database being used by Apache…

To use mysqli or PDO?

I’m trying to make my mind up about whether I should be using mysqli or PDO for a project I’m just starting and I’ve found a few relevant articles:

And based on all of that I think I’ve decided to use PDO.