LivinInChina Posted September 1, 2012 Share Posted September 1, 2012 Hi everyone! After following a tutorial about the MVC architecture, I'm having some trouble using a certain class to connect to my db, make a query with SELECT and displays it with a foreach. Actually, I succeeded to connect and make the query, however it's when comes the moment to display the result that I get the following 2 errors: SCREAM: Error suppression ignored for Warning: Invalid argument supplied for foreach() So here is the connection class using PDO and singleton: <?php class PDO2 extends PDO { private static $_instance; public function __construct( ) { } // End of PDO2::__construct() */ /* Singleton */ public static function getInstance() { if (!isset(self::$_instance)) { try { self::$_instance = new PDO(SQL_DSN, SQL_USERNAME, SQL_PASSWORD); } catch (PDOException $e) { echo $e; } } return self::$_instance; } // End of PDO2::getInstance() */ } // end of file */ ?> Here is a part of the code from the tutorial that's used as a function to make a query: <?php function lire_infos_utilisateur($id_utilisateur) { $pdo = PDO2::getInstance(); $requete = $pdo->prepare("SELECT nom_utilisateur, mot_de_passe, adresse_email, avatar, date_inscription, hash_validation FROM membres WHERE id = :id_utilisateur"); $requete->bindValue(':id_utilisateur', $id_utilisateur); $requete->execute(); if ($result = $requete->fetch(PDO::FETCH_ASSOC)) { $requete->closeCursor(); return $result; } return false; } ?> And here is how the query function is used to be displayed: <?php if (false !== $id_utilisateur) { $infos_utilisateur = lire_infos_utilisateur($id_utilisateur); // On enregistre les informations dans la session $_SESSION['id'] = $id_utilisateur; $_SESSION['pseudo'] = $nom_utilisateur; $_SESSION['avatar'] = $infos_utilisateur['avatar']; $_SESSION['email'] = $infos_utilisateur['adresse_email']; } ?> But this is my question: how make my following script working? <?php function listing_fonts() { $pdo = PDO2::getInstance(); $requete = $pdo->prepare("SELECT * FROM fonts"); $requete->execute(); if ($result = $requete->fetch(PDO::FETCH_OBJ)) { $requete->closeCursor(); return $result; } return false; } $listing_fonts = listing_fonts(); if (false !== $listing_fonts) { foreach($listing_fonts as $font) //Affichage des fonts trouv?es en image { var_dump($font); } } ?> So with this scritp I get the error I show above. So obviously there is something that I'm not doing right but I can't figure what and I'm desperate. Does anybody has an answer??? Thanks a lot in advance for all your help! Quote Link to comment https://forums.phpfreaks.com/topic/267892-using-a-class-with-singleton-to-make-a-query-on-the-db-coding-with-pdooop/ Share on other sites More sharing options...
darkfreaks Posted September 1, 2012 Share Posted September 1, 2012 change: //value not equals FALSE if (false !== $listing_fonts) {] To: //if array value exists if(is_array($listing_fonts) { } Quote Link to comment https://forums.phpfreaks.com/topic/267892-using-a-class-with-singleton-to-make-a-query-on-the-db-coding-with-pdooop/#findComment-1374540 Share on other sites More sharing options...
LivinInChina Posted September 2, 2012 Author Share Posted September 2, 2012 Ok I finally got it after several hours of online research an tests. I'm not gonna give you details it's been quite a work! ^^ Quote Link to comment https://forums.phpfreaks.com/topic/267892-using-a-class-with-singleton-to-make-a-query-on-the-db-coding-with-pdooop/#findComment-1374637 Share on other sites More sharing options...
darkfreaks Posted September 2, 2012 Share Posted September 2, 2012 @ OP: please mark topic as solved Quote Link to comment https://forums.phpfreaks.com/topic/267892-using-a-class-with-singleton-to-make-a-query-on-the-db-coding-with-pdooop/#findComment-1374638 Share on other sites More sharing options...
Christian F. Posted September 2, 2012 Share Posted September 2, 2012 Just a little comment in closing, now that you've gotten the original problem out of the way. public function __construct( ) { The whole point about singletons is that their constructors are private, so that it is impossible to create a new instance of the class. You also want to set the __clone () method private, or have it return the One True Instance. () Though, it is recommended to disable it, so that there will be no confusion. Quote Link to comment https://forums.phpfreaks.com/topic/267892-using-a-class-with-singleton-to-make-a-query-on-the-db-coding-with-pdooop/#findComment-1374719 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.