Question: In Lucene.Net how do I retrive a single Document based on a Field value? The Field value will always be unique to a single document in this case.

The Field I wish to use is "Id" from this is the structure of my document:

```
var doc = new Document();

// Add lucene fields mapped to DB fields
doc.Add(new Field("Id", searchResult.Id, Field.Store.YES, Field.Index.NOT_ANALYZED));
doc.Add(new Field("Name", searchResult.Name, Field.Store.YES, Field.Index.ANALYZED));
doc.Add(new Field("Region", searchResult.Region, Field.Store.YES, Field.Index.ANALYZED));
doc.Add(new Field("Type", searchResult.Type, Field.Store.YES, Field.Index.NOT_ANALYZED));
doc.Add(new Field("Permalink", searchResult.Permalink, Field.Store.YES, Field.Index.NOT_ANALYZED));

```

I can find lots of examples of searching Lucene for multiple results, but nothing for retrieving a single item. I'm sure it must be possible as I imagine it would be required for updating only a specific Document in Lucene.

I imagine the first step is to change the "Id" Field to ANALYZED rather than NOT\_ANALYZED and rebuild the index.

I feel that there is probably a nice simple method I haven't stumbled across yet for retrieving a single Document rather than using a QueryParser?

Answer: 
I advise you to check the documentation on [PDO](http://php.net/manual/fr/class.pdo.php) and [PDOStatement](http://php.net/manual/fr/class.pdostatement.php) for more detailed information. 

I guess you're already connected to your database through PDO ? (else see PDO::\_\_construct())

With a prepared query you better "bind" ($sthandler->bindParam()) your parameters instead of concatenating them, like so

```
$sthandler = $conn->prepare('SELECT userid FROM users WHERE userid= :user_id');
$sthandler->bindParam(':userid', $userid, PDO::PARAM_INT);

```

I don't quite understand the rest of your code though. Maybe isn't it complete. You make a loop on something that is supposed to always be a unique result (i hope for you, "userid" is a unique primary key). 

If this loop is actually useless, then maybe you should consider making only 1 query to get both user and courses at the same time, by using a join between the tables :

```
SELECT u.userid, c.crn FROM users u INNER JOIN usercourses c ON u.userid = c.userid WHERE u.userid= :user_id

```

And from this result, get anything you want. Yes you will have duplicated data in your result (userid on each row of different courses), but it's still a better way than doing 2 seperate queries.

Consider using `$sthandler->fetch()` or `$sthandler->fetchAll()` to get every results and organize them into an array, as it suits you :

```
$rows = $sthandler->fetchAll(PDO::FETCH_ASSOC); // FETCH_ASSOC will return the results as array with columns' name as keys.
// Do your loop on the array $rows to process it

```

Or

```
while( $row = $sthandler->fetch(PDO::FETCH_ASSOC) ) {
     // do whatever you like with each row
}

```

Also take Darren's advice to wrap your PDO queries with `try/catch` to avoid any errors.
The point of PDO is to prevent concatenating the query string like you're currently doing. It's supposed to be used used to prepare the statements.

```
$sthandler = $conn->prepare('SELECT userid FROM users WHERE userid= :user_id');
if($sthandler->execute(array(':user_id' => $user_id))) {
    if($sthandler->rowCount() > 0)
    {
        // User exists, get user's courses from database and returns array of CRNs
        for(int $i = 0; $i < rowCount; $i++)
        {
            $sthandler = $conn->prepare('SELECT crn FROM usercourses WHERE userid= :user_id';
            $sthandler->execute(array(':user_id' => $user_id));
        }            
    }
}

```

You're trying to execute with random data in the arrays.

One more thing, you should wrap your queries in a `try/catch` statement, to handle any potential errors:

```
try {
    // run query and all
}
catch (PDOException $e){
    // echo out the error if there is one.
    echo $e->getMessage();
}

```

And the last note, while you're at it. You should explicitly turn on errors when constructing your PDO object!

```
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

```