KarlvonKarton Posted December 22, 2016 Posted December 22, 2016 function getKappers($limit = 10){ $results_array = array(); $rs = wire('database')->prepare("SELECT * FROM appointment_kapper WHERE (actief = '1') ORDER BY volgorde LIMIT ?"); $rs->bindParam(1, $limit, PDO::PARAM_INT); // ERROR $rs->execute([$limit]); while ($row = $rs->fetch()){ $results_array[] = $row; } return $results_array; } Gives me this error and I don't understand why. Error: Class 'ProcessWire\PDO' not found Anyone? I suspect PDO::PARAM_INT ?
szabesz Posted December 22, 2016 Posted December 22, 2016 Hi, Namespace issue? http://stackoverflow.com/questions/19699319/php-namespace-pdo-not-found use \PDO; 2
KarlvonKarton Posted December 22, 2016 Author Posted December 22, 2016 4 minutes ago, szabesz said: Hi, Namespace issue? http://stackoverflow.com/questions/19699319/php-namespace-pdo-not-found use \PDO; Tried that already, with no success. But since I am using namespace, I thought it was not needed: <?php namespace ProcessWire;
adrian Posted December 22, 2016 Posted December 22, 2016 The problem is that PDO is outside the PW namespace, which is why the "\" is necessary to get you to the global namespace. That change definitely works for me. But then you'll get an error with the way you have your execute([$limit]); I would go with this version instead: function getKappers($limit = 10) { $rs = wire('database')->prepare("SELECT * FROM appointment_kapper WHERE (actief = '1') ORDER BY volgorde LIMIT :limit"); $rs->bindParam(':limit', $limit, \PDO::PARAM_INT); $rs->setFetchMode(\PDO::FETCH_ASSOC); $rs->execute(); return $rs->fetchAll(); } Note that I also used FETCH_ASSOC so you automatically get an array back without needing to iterate the results to build it up. 3
KarlvonKarton Posted December 22, 2016 Author Posted December 22, 2016 Oh! Silly me... I forgot that the $limit in the execute was still there... (that's what you get with C/P-ing) And of course yes, now the backslash is the solution. @adrian: correct, I should use FETCH_ASSOC (and maybe :limit too) 1
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now