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â;
BEWARE:
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.