Alex Rivera | Logout

get number of rows with pdo

Asked 2012-11-02T13:13:19.207
12

I have a simple pdo prepared query:

$result = $db->prepare("select id, course from coursescompleted where person=:p"); 
$result ->bindParam(':p', $q, PDO::PARAM_INT);
$result->execute();
$rows = $result->fetch(PDO::FETCH_NUM);
echo $rows[0];

the echo seems to be returning the ID value of the record, not the number of records returned by the query?

any idea or explanation for this?

Edit
Report

1 Answer

0

Try echo count($rows); as $rows is an array.

Edit:

To use the results

$rows = $result->fetchAll(/* nothing here */);
if(count($rows) > 0) {
  // Show results, perhaps using a foreach($rows as $row)
} else {
 echo "Sorry, no results found";
}
answered 2012-11-02T13:15:13.883

Your Answer