joshuahornby10
joshuahornby10

Reputation: 4302

How to get a PDO Fetch( ) to return as string

How can I get a PDO to return the data as a string not an array? I am not sure this is possible so if not is there a way to convert the array to a string after it has been processed?

My code which is returning the string is:

$result = $stmt->fetch(PDO::FETCH_ASSOC);
print_r($result);

I have tried different fetch types such as FETCH_CLASS FETCH_OBJ

Upvotes: 7

Views: 16996

Answers (2)

Abdulsalam Moshood
Abdulsalam Moshood

Reputation: 1

$result = $statement->fetchColumn();

This work for me, the only thing I noticed here is that fetchColumn() always refer to first column on your table. You need to adjust / re-position the only column needed to the first-column on your table or make an Sql query i.e "SELECT" query to get the only column needed. Thanks

Upvotes: 0

Joseph at SwiftOtter
Joseph at SwiftOtter

Reputation: 4321

You can do it like this:

echo $stmt->fetchColumn();    

Or:

$result = $stmt->fetch(PDO::FETCH_ASSOC);
echo $result[0];

Upvotes: 16

Related Questions