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 ^^ '

SatanicGeek
  • 342
  • 5
  • 15
  • 3
    You're basically reinventing the wheel. Drop the deprecated `mysql_` functions and use [mysqli](http://www.php.net/manual/en/class.mysqli.php) instead. It already does all that and more. – Machavity Apr 09 '15 at 15:46

3 Answers3

4

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

1

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

for the __construct function must be public.

Machavity
  • 30,841
  • 27
  • 92
  • 100
RRahmouni
  • 24
  • 3
0

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

$oMySQL = new MySQL();
juco
  • 6,331
  • 3
  • 25
  • 42
claudiosan
  • 34
  • 2
  • 1
    You don't have to use () if, for example, you set nothing to __construct() – sergio Apr 09 '15 at 15:48
  • 1
    Read this (using parentheses is not required): http://stackoverflow.com/questions/1945989/php-class-instantiation-to-use-or-not-to-use-the-parentheses – Whirlwind Apr 09 '15 at 15:49