Jump to content

Using a class with singleton to make a query on the db - Coding with PDO/OOP


LivinInChina

Recommended Posts

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!

Link to comment
Share on other sites

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. (:P) Though, it is recommended to disable it, so that there will be no confusion.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.