I use PDO and prepare all my statements primarily for security reasons. However, I have part of my code that executes the same statement with different parameters many times, and I thought that this would be where the prepared statements really get prepared. But they really break the code ...
The basic logic of the code is this.
function someFunction($something) { global $pdo; $array = array(); static $handle = null; if (!$handle) { $handle = $pdo->prepare("A STATEMENT WITH :a_param"); } $handle->bindValue(":a_param", $something); if ($handle->execute()) { while ($row = $handle->fetch()) { $array[] = someFunction($row['blah']); } } return $array; }
It looked good to me, but it lacked a lot of lines. In the end, I realized that the operator descriptor was changed (executed with another parameter), which means that the call to retrieve in the while loop will work only once, then the function will call itself again, and the result set will be changed.
So, I am wondering what is the best way to use prepared PDO instructions in a recursive way.
One way might be to use fetchAll (), but the manual contains a substantial overhead. The thing is to make it more effective.
Another thing I can do is not to repeat the static descriptor, but instead do a new one every time. I believe that since the query string is the same, internally, the MySQL driver will use the prepared statement anyway, so there is only a small overhead for creating a new descriptor for each recursive call. Personally, I think it wins.
Or is there a way to rewrite this?
php mysql pdo recursion prepared-statement
Rob
source share