Fetching Single Row, Single Column with Pdo

Fetching single row, single column with PDO

Are you sure it's returning any rows?

$stmt->fetchColumn()

is correct way to fetch a single value, so either you probably didn't bind the :user parameter or it simply returned no rows.

MySQL pdo select single column with limit but return one row

fetch retrieves one row, or it returns FALSE if there is no row to fetch. (We could do the processing in a loop, and fetch each "next row", until fetch returns FALSE.

  $query->execute();
while ( $row = $query->fetch(PDO::FETCH_ASSOC) ) {
echo $row['title'];
}

There's only one column in the resultset. It seems like we are confused about fetch and fetchAll.

http://php.net/manual/en/pdostatement.fetch.php

http://php.net/manual/en/pdostatement.fetchall.php


If we use fetchAll to retrieve all of the rows, we get an array back, each element in the array is a row from the resultset.

  $query->execute();
if( $rs = $query->fetchAll(PDO::FETCH_ASSOC) ) {
foreach($rs as $row) {
echo $row['title'];
}
}

PHP PDO returning single row

Just fetch. only gets one row. So no foreach loop needed :D

$row  = $STH -> fetch();

example (ty northkildonan):

$id = 4;
$stmt = $dbh->prepare("SELECT name FROM mytable WHERE id=? LIMIT 1");
$stmt->execute([$id]);
$row = $stmt->fetch();

How to get column names and values from a single-row resultset with PDO?

No need to do array_search(), do like below:-

function story($id) {
global $db;
$sql = "select * from users where id = :aid limit 1";
$st = $db->prepare($sql);
$st -> execute([":aid" => $id]);
$row = $st->fetch(PDO::FETCH_ASSOC);
if(count($row)>=1){
foreach ($row as $column => $value) {
echo "<div class='title'>" . $column . "</div>\n" .
echo "<div class='story'>" . $value. "</div>\n";
}
}
}

fetch single record using PHP PDO and return result

Let me suggest you to change this approach a bit, to make it A LOT more flexible.

  • First, you definitely have to make this function accept an array with data for execute(). Otherwise there will be no sense in using prepare or PDO at all.
  • Then, make your function return the statement. It will make it enormously flexible

So, change the code to this

function getdata($dbh, $sql, $params = NULL)
{
$stmt = $dbh->prepare($sql);
$stmt->execute($params)
return $stmt;
}

this way you'll be able to fetch either single record.

$row = getdata($dbh, $sql)->fetch();

or multiple rows

$row = getdata($dbh, $sql)->fetchAll();

or even run insert or update queries from which you cannot fetch at all.

PDO fetch one column from table into 1-dimensional array

<?php
$sql = "SELECT `ingredient_name` FROM `ingredients`";
$ingredients = $pdo->query($sql)->fetchAll(PDO::FETCH_COLUMN);

Why does PDO fetch() return only the first row?

To select all rows using PDO, you need to use fetchAll() instead of fetch().

$e = $db->query("SELECT `username`, `membership` FROM `users` WHERE `expire` != ''")->fetchAll();

In PDO, fetch() returns "the next row from a result set".

fetchAll() returns "an array containing all of the result set rows".

PHP PDO query: fetch a field from the first row

You can use fetchColumn() to return a single column from the next row in the result set. If there is only one column in the result set, do:

$desired_field = $result->fetchColumn();

If there are multiple columns in the result set, specific the numeric index of the one you want:

$desired_field = $result->fetchColumn(1);

Take notice of the warning provided in the fetchColumn() documentation:

There is no way to return another column from the same row if you use PDOStatement::fetchColumn() to retrieve data.



Related Topics



Leave a reply



Submit