0

I have a PHP class that I use to connect to my database. When I call it, nothing is displayed on my page. No PHP/MySQL error ...

I'm stuck on this for a while and I 'm tired ...

Can anyone help me?

Here is my class :

    class MySQL {

        // Adresse MySQl
        private $sHost = 'localhost';
        private $sPort = '8889';

        // Nom et connexion de la db
        private $sDB = 'db_Stefano';
        private $cConnexion;

        // Identifiants de connexion
        private $sUsername = 'Stefano';
        private $sPassword = 'Soleil1234';

        /* ------------------------------------------------------------------ */

        // Connexion à la base de données
        private function __construct () {

            // Connexion à MySQL
            $this->cConnexion = mysql_connect($this->sHost.':'.$this->sPort, $this->sUsername, $this->sPassword);

            if(!$this->cConnexion) {
                die('Connexion à la base de données impossible.<br>Erreur : '.mysql_error());
            }

            // Séléction de la base de données
            mysql_select_db($this->sDB, $this->cConnexion);

        }

        /* ------------------------------------------------------------------ */

        // Déconnexion à la base de données
        public function close () {
            mysql_close($this->cConnexion);

        }

        /* ------------------------------------------------------------------ */

        // Exécution des requêtes
        public function query ($sRequete) {
            $this->cConnexion = mysql_query($sRequete, $this->cConnexion) or die('Impossible d\'exécuter la requête.<br>Erreur : '.mysql_error());

            return $this->cConnexion;

        }

        /* ------------------------------------------------------------------ */

        // Met en forme les valeurs reçues
        public function fetch ($cConnexion = null) {
            $aValue = array();

            while($sValue = mysql_fetch_assoc(($cConnexion != null ? $cConnexion : $this->cConnexion))) {
                $aValue[] = $sValue;
            }

            return $aValue;

        }

        /* ------------------------------------------------------------------ */

        // Compte le nombre de valeurs
        public function count ($cConnexion = null) {
            return mysql_num_rows(($cConnexion != null ? $cConnexion : $this->cConnexion)) or die('Impossible de compter le nombre de lignes.<br>Erreur : '.mysql_error());

        }
    }

And the appeal of the class and its functions :

// Objet de connexion à la base de données
$oMySQL = new MySQL;

$sReqUsers = 'SELECT * FROM t_users';

$aUsers = $oMySQL->query($sReqUsers);
$aUsers = $oMySQL->fetch($aUsers);

print_r($aUsers);

I speak French so feel free to correct me for school English ^^ '

1
  • 3
    You're basically reinventing the wheel. Drop the deprecated mysql_ functions and use mysqli instead. It already does all that and more. Commented Apr 9, 2015 at 15:46

3 Answers 3

4

Your __construct method is private, so you can't instatiate your class with "new...". Put your construct in public.

Sign up to request clarification or add additional context in comments.

Comments

1

if you have no errors, try using error_reporting(E_ALL) to see errors first.

for the __construct function must be public.

Comments

0

I think that you have forgotten the () after instantiating the class

$oMySQL = new MySQL();

2 Comments

You don't have to use () if, for example, you set nothing to __construct()
Read this (using parentheses is not required): stackoverflow.com/questions/1945989/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.