Results of a PDO query not displaying

ⅰ亾dé卋堺 提交于 2021-01-28 06:28:26

问题


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


回答1:


  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;



回答2:


  $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



来源:https://stackoverflow.com/questions/13837520/results-of-a-pdo-query-not-displaying

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!