Getting one row from a MySQL query

Often you just need one value or one row of values from a table. If your query returns a unique row because of the query or because you limited it to one row, then you don’t need to go through the foreach loop to get the values. You can change the fetchAll() to fetch() and use just one line to get the value.

// get the current category name
$qry  = "SELECT name FROM `website`.`book_category` ";
$qry .= "WHERE id = :categoryID";

$stmt = $dbWG->prepare($qry);
$stmt->bindParam(':categoryID', $categoryID);

$results = $stmt->fetch();
$categoryName = $results['name'];

Leave a Reply

You must be logged in to post a comment.

Well Golly

Atheism Plus

Buy from Amazon