Take the 2-minute tour ×  I've tried following the PHP.net
instructions for doing Select queries but I am not sure the best
way to go about doing this.  I would like to use a parameterized
Select query, if possible, to return the ID in a table where the
name field matches the parameter. This should return one ID
because it will be unique.  I would then like to use that ID for
an Insert into another table, so I will need to determine if it
was successful or not.  I also read that you can prepare the
queries for reuse but I wasn't sure how this helps.
share|improve this question  4 Answers 4  up vote 74 down vote
accepted  You select data like this:  $db = new PDO("...");
$statement = $db->prepare("select id from some_table where name
= :name"); $statement->execute(array(':name' => "Jimbo")); $row
= $statement->fetch(); // Use fetchAll() if you want all
results, or just iterate over the statement, since it implements
Iterator  You insert in the same way:  $statement =
$db->prepare("insert into some_other_table