简体   繁体   中英

Results of a PDO query not displaying

I'm trying to make a PDO query to display data. This is what i have done so far :

in my models/pdo, i created a class with this:

<?php

class VengeanceUsers {

    public static function getNumbersOfregistered()
    {
      $connexion = new PDO("mysql:host=localhost ;dbname=databasetest", 'root', 'passe'); // connexion à la BDD

      $var_dump($connexion);
      exit();
      $resultats=$connexion->query("SELECT COUNT (*) FROM ope_tartine_nl "); // on va chercher tous les membres de la table qu'on trie par ordre croissant
      return $resultats;
    }

}

?>

In my controller:

$this->view->nb_users = VengeanceUsers::getNumbersOfregistered();

In my view:

Nombre d'inscrits : <?php echo $this->nb_users; ?><br/>

I don't have anything displayed... Can anyone help me on this ? Thanks in advance

  $resultats=$connexion->query("SELECT COUNT (*) FROM ope_tartine_nl "); // on va chercher tous les membres de la table qu'on trie par ordre croissant

This only gives you the result object of the query.

You have to actually fetch the row from your result.

  $resultats=$connexion->query("SELECT COUNT (*) FROM ope_tartine_nl "); // on va chercher tous les membres de la table qu'on trie par ordre croissant
  $numRows = $resultats->fetchColumn();
  return $numRows;

This should fix it

  1. You forgot to comment exit(); out.
  2. In $var_dump($connexion); you need to remove $ in the beginning.
  3. You need to fetch results from the statement.

For #3 you can use:

$sth = $connexion->query("SELECT COUNT (*) FROM ope_tartine_nl "); // get statement
$resultats = $sth->fetchColumn(); // get data
return $resultats;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM