It’s nothing to do with PDO - as far as I know (from limited usage) mysqli would behave the same way, as would the old mysql_ functions.
I suspect what may be confusing you is that you only requested one column from the query, but it still comes back as an array when you use fetch(). You could use fetchColumn() when you’re only retrieving a single value, and then it wouldn’t return an array.
There’s no need to guess. The error message tells you what line the error is on, so you can be pretty sure that is the line causing the problem. The message “Array to string conversion” makes it pretty clear that it’s having to convert an array to a string to do whatever you are doing on that line, which in this case is just trying to echo it. The next line “Array” is what you get when you try to echo an array in PHP - PHP just converts your array (for display purposes) to a string containing the word “array”, so you do at least get something out of your echo.
This is a bit off-topic but once you have figured out how to get your numeric value then it might be worthwhile to run the query again with ATTR_EMULATE_PREPARES set to true or even just commented out. There is a subtle but important distinction between the values returned.
It has very little to do with PDO. @droopsnoot and @chorn are right. It’s an array conversion problem. The thing I like about PHP’s errors and warnings is that PHP makes it extremely obvious as to why you’re getting those errors. This typically means it’s a developer problem. Just like in any other language, there are strict rules to fiddling with data types. Even though a lot of people think PHP is a loosely typed language, it still has certain restrictions on what you can and can’t do.
That being said, if you want to properly output each individual result, you have to run it through a loop or else use some kind of output function that accepts arrays. Both echo and print accepts only strings. var_dump and print_r and various other functions will allow you to pass in arrays, but will output the entire result as an array list.
The function has it’s own file and called with require ‘vd.php’;
If on the off-chance you are not familiar with PHP print_r() and var_dump() functions they have the advantage of displaying every type of variable whereas echo FAILS when trying to output arrays and objects.
In your example code4, you are retrieving two columns (that is, your query will return two columns), so you clearly cannot use the function that is only useful to retrieve a single column. When you have more than one column, you retrieve them as an array, as you were originally doing, and then use the contents of that array.