purencool Posted May 30, 2011 Share Posted May 30, 2011 hi phpfreaks, I have been using learning the pdo library. But I am really stuck on this one query. The above sql statement works if in phpadmin I get three results but for the life of me I can't get them with the code below. I have tried I am trying to return all columns in an array some thing like array (0=>array(0=>business blah, etc ) I have been playing with the fetch mode to try and achieve this but I can't get any results not even an obj but I am not getting any errors. Is the code below wrong? $query = "SELECT Business_Name,First_Name,Last_Name,Username,Email,Phone,Mobile,Social_Media,Web_Site,User_Language,Admin_Level FROM customer WHERE Admin_Level ='Admi'"; try { //$this->dataObj->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $this->dataObj->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING ); $sth = $this->dataObj->prepare($query); $stmt = $this->dataObj->query($query); $stmt->setFetchMode(); $result = $stmt->fetchAll(); } catch (PDOException $e) { $e->getMessage(); } return $result; } Quote Link to comment https://forums.phpfreaks.com/topic/237814-pdo-issue/ Share on other sites More sharing options...
mgoodman Posted May 30, 2011 Share Posted May 30, 2011 I've never used setFetchMode() before but it does take an argument which you have not supplied. http://php.net/manual/en/pdostatement.setfetchmode.php That could be the problem. Put this at the top of your file: ini_set('display_errors', 1); error_reporting(E_ALL); and see if it displays any errors when you try to open the page. Quote Link to comment https://forums.phpfreaks.com/topic/237814-pdo-issue/#findComment-1222078 Share on other sites More sharing options...
mikesta707 Posted May 30, 2011 Share Posted May 30, 2011 WHERE Admin_Level ='Admi'"; should this be WHERE Admin_Level ='Admin'"; I don't know much about PDO but that was the first thing I noticed Quote Link to comment https://forums.phpfreaks.com/topic/237814-pdo-issue/#findComment-1222079 Share on other sites More sharing options...
btherl Posted May 30, 2011 Share Posted May 30, 2011 Does another simpler query work? Such as one with no condition on Admin_Level? If yes then you can be confident that your PDO usage is correct, and focus on the query itself. Quote Link to comment https://forums.phpfreaks.com/topic/237814-pdo-issue/#findComment-1222080 Share on other sites More sharing options...
purencool Posted May 30, 2011 Author Share Posted May 30, 2011 I have been trying your ideas and have modified the code a little but still not luck my results -trying a smaller query. In phpadmin works perfectly three results. -admi is correct thanks for idea. -removed fetch mode and made no difference still get the same result. -added PDO::FETCH_ASSOC to fetch all. current code as with changes any other ideas? private function QryFtchClmns($query){ $query = "SELECT Admin_Level FROM customer WHERE Admin_Level ='Admi'"; try { //$this->dataObj->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $this->dataObj->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING ); $stmt= $this->dataObj->prepare($query); $stmt = $this->dataObj->query($query); //$stmt->setFetchMode(); $result = $stmt->fetchAll(PDO::FETCH_ASSOC); } catch (PDOException $e) { $e->getMessage(); } return $result; } Quote Link to comment https://forums.phpfreaks.com/topic/237814-pdo-issue/#findComment-1222085 Share on other sites More sharing options...
btherl Posted May 30, 2011 Share Posted May 30, 2011 The way to use prepare is demonstrated on the manual page here: http://www.php.net/manual/en/pdo.prepare.php $sth = $dbh->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY)); $sth->execute(array(':calories' => 150, ':colour' => 'red')); But your code should work with just the query, even though the prepare is not doing anything. Does your code return an empty array? If so, try without the "where" condition. Quote Link to comment https://forums.phpfreaks.com/topic/237814-pdo-issue/#findComment-1222094 Share on other sites More sharing options...
purencool Posted May 30, 2011 Author Share Posted May 30, 2011 ok I modified the code with a place holder as below and got this Array ( [0] => [1] => 0 [2] => 0 [3] => 0 [4] => 0 [5] => 0 [6] => 0 [7] => 4 [8] => 2 [9] => 1 ) private function QryFtchClmns($query){ $sql = "SELECT Admin_Level FROM customer WHERE Admin_Level = ?"; $query = 'Admi'; try { //$this->dataObj->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $this->dataObj->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING ); $stmt= $this->dataObj->prepare($sql); $stmt = $this->dataObj->query($query); //$stmt->setFetchMode(); $result = $stmt->fetchAll(PDO::FETCH_ASSOC); } catch (PDOException $e) { $e->getMessage(); } return $result; } Quote Link to comment https://forums.phpfreaks.com/topic/237814-pdo-issue/#findComment-1222106 Share on other sites More sharing options...
purencool Posted May 30, 2011 Author Share Posted May 30, 2011 :D :D :D thanks for all your help. I had two issues on is my pdo syntax and the public get method was not calling the private method I have online was it was calling another pdo method in same class and it had similar issues. As so as I called the method I have here on display the errors came thick and fast. Thanks for your help again everyone. Quote Link to comment https://forums.phpfreaks.com/topic/237814-pdo-issue/#findComment-1222120 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.