问题
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:
- You forgot to comment
exit();
out. - In
$var_dump($connexion);
you need to remove$
in the beginning. - 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