2013-02-01 3 views
0

comment puis-je obtenir le résultat de ma requête COUNT.comment obtenir la valeur comptée

c'est à quoi il ressemble de ma requête dans ma base de données

fname lname mname positionName COUNT(tbl_votes.studId) 
    jr gwapo is-very chairman   2 

et voilà comment ma page Web ressemble

 Name   Position  Number of Votes 
    jr is-very gwapo chairman   ______ 

et Heres mon code.

<?php 
    if ($result = $mysqli->query("SELECT tbl_student.fname, tbl_student.lname, tbl_student.mname, tbl_position.positionName, Count(tbl_votes.studId) FROM tbl_candidate Inner Join tbl_student ON tbl_candidate.studId = tbl_student.studId Inner Join tbl_position ON tbl_candidate.positionId = tbl_position.positionId Inner Join tbl_votes ON tbl_student.studId = tbl_votes.candId WHERE tbl_position.positionId = '1' GROUP BY tbl_student.fname, tbl_student.lname, tbl_student.mname, tbl_position.positionName")) { 

     if ($result->num_rows > 0) { 
      echo "<table border='1' cellpadding='10'>"; 
      // set table headers 
      echo "<tr><th>Name</th><th>Position</th><th>Number of Votes</th></tr>"; 

      while ($row = $result->fetch_object()) { 
       echo "<tr>"; 
       echo "<td>" . $row->fname . " " . $row->mname . " " . $row->lname . " </td>"; 
       echo "<td>" . $row->positionName . "</td>"; 
       //this is where i suppose to echo the count result 
      echo "<td>" . $row-> ??? . "</td>"; 
      echo"<tr>"; 
      } 
      echo "</table>"; 
     } else { 
      echo "No results to display!"; 
     } 

    } 
    $mysqli->close(); 

?> 

Heres mon problème, comment pourrais-je passer le "comte (tbl_votes.studId)" dans "echo "" $ ligne-> ??? ".";"? pls help ...

+0

@VladPreda merci beaucoup pour fixer l'empreinte dans la question. –

+0

Pas de problème. Je vous recommande d'utiliser un IDE qui le fait automatiquement. Je viens de le copier et de le recopier :) –

+0

à tous ..thnks pour répondre ... –

Répondre

1

En requête SQL, remplacez Count(tbl_votes.studId) par Count(tbl_votes.studId) as stu_count.

Et en php, vous pouvez utiliser $row->stu_count

+0

@pktangyue .. thnks pour répondre .. –

0

Vous devez utiliser un SQL alias.

Count(tbl_votes.studId) as cnt 
//or 
Count(tbl_votes.studId) 'cnt' 

Ensuite, vous pouvez y accéder avec $row->cnt

Quelques règles sur les alias:

  • vous pouvez utiliser un alias dans le reste de votre requête (dans votre exemple, vous pouvez utiliser ORDER BY cnt). Note: pour autant que je sache, vous ne pouvez pas utiliser un alias que vous avez créé dans le select dans une autre instruction select. Par exemple, SELECT COUNT(something) AS cnt, cnt+3 FROM ... ne fonctionnera pas
  • vous pouvez utiliser alias pour les tables, et remplacer le nom de la table avec l'alias dans le futur usages
+0

..thnks pour répondre –

0

Essayez field1 Sélectionnez, field2, Count (tbl_votes.studId) comme cnt de ...

0

au lieu d'écrire

count(some_field) 

écrire

count(some_field) as count_some_field 

Vous attribuez un alias à ce champ de comptage. Vous pouvez y accéder comme $ row-> count_some_field.

0

vous devez utiliser 'as' avec 'count'. Alors vous interrogez deviendra comme cette

"SELECT tbl_student.fname, tbl_student.lname, tbl_student.mname, tbl_position.positionName, Count(tbl_votes.studId) as no_of_votes FROM tbl_candidate Inner Join tbl_student ON tbl_candidate.studId = tbl_student.studId Inner Join tbl_position ON tbl_candidate.positionId = tbl_position.positionId Inner Join tbl_votes ON tbl_student.studId = tbl_votes.candId WHERE tbl_position.positionId = '1' GROUP BY tbl_student.fname, tbl_student.lname, tbl_student.mname, tbl_position.positionName" 

alors vous pouvez l'obtenir via php comme

$row->no_of_vote

pour mor informations, voir COUNT AS