选择特定的重复数据库值并显示计数

时间:2014-01-21 19:43:54

标签: php mysql select count where

我尝试了一百万种不同的方式,这可能是一件非常简单的事情,但我无法理解它。

我有一个数据库,显示几个不同的列(名称,年龄,性别,品种,玩家等)

我想要的是包含一行“我们有___个女性角色。”

这是我想要显示的整个页面:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
 <title>AUSUS --- mythical equine RPG</title>
 <link rel="stylesheet" type="text/css" href="/css/evolution2.css" />
<link href='http://fonts.googleapis.com/css?family=Vollkorn:400,400italic,700,700italic|Kreon:400,700,300|La+Belle+Aurore|Stalemate|IM+Fell+English:400,400italic|Mrs+Saint+Delafield' rel='stylesheet' type='text/css'>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>

<h1>Character Database</h1>


<p>
We currently have <b>

<?php

$link = mysql_connect("***", "***", "***");
mysql_select_db("tarb89_characters", $link);
$result = mysql_query("SELECT * FROM characters", $link);
$num_rows = mysql_num_rows($result);
echo "$num_rows";

?>

</b>
characters listed in the database.  You may search the character database by character name or player name.  Dams and sires marked with an asterisk (*) are from <b>outside</b> Ausus.<br><br>




 <br><br>
<form action="searchchars.php" method="GET"/>
<input type="text" name="searchchars" />
<input type="submit" name="submit" value="Character Name Search" /><br>
</form>
<br>
<form action="searchplays.php" method="GET"/>
<input type="text" name="searchplays" />
<input type="submit" name="submit" value="Player Name Search" />
</form>
</p>

<p>
<br><br>
<?php
/// In order to use this script freely
/// you must leave the following copyright
/// information in this file:
/// Copyright 2012 www.turningturnip.co.uk
/// All rights reserved.

include("connect.php");

if (isset($_GET["page"])) { $page  = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * 20; 
$result = mysql_query("SELECT * FROM characters ORDER BY name ASC LIMIT $start_from, 20");
$num = mysql_num_rows ($result);

if ($num > 0 ) {
$i=0;
while ($i < $num) {

$id = stripslashes(mysql_result($result,$i,"id"));
$name = stripslashes(mysql_result($result,$i,"name"));
    $breed = stripslashes(mysql_result($result,$i,"breed"));
    $gender = stripslashes(mysql_result($result,$i,"gender"));
    $color = stripslashes(mysql_result($result,$i,"color"));
    $markings = stripslashes(mysql_result($result,$i,"markings"));
    $genetics = stripslashes(mysql_result($result,$i,"genetics"));
    $player = stripslashes(mysql_result($result,$i,"player"));
    $traits = stripslashes(mysql_result($result,$i,"traits"));
    $defects = stripslashes(mysql_result($result,$i,"defects"));
    $extras = stripslashes(mysql_result($result,$i,"extras"));
    $profile = stripslashes(mysql_result($result,$i,"profile"));
    $dam = stripslashes(mysql_result($result,$i,"dam"));
    $sire = stripslashes(mysql_result($result,$i,"sire"));
    $status = stripslashes(mysql_result($result,$i,"status"));

$row .= '

<h5>'.$name.'</h5>

<table width="550px" cellpadding="5" cellspacing="20" style="background: #eeeeee; border-radius: 15px; border: 1px solid #5067be;">
<tr>
<td width="50%"><p>
    <b>ID Number:</b>  '.$id.'<br>
    <b>Breed:</b>  '.$breed.'<br>
    <b>Gender:</b>  '.$gender.'<br>
    <b>Dam:  </b>'.$dam.'<br>
    <b>Sire:  </b>'.$sire.'<br>
    <b>Status:  </b>'.$status.'<br>
    <b>Profile:</b>  <a href="'.$profile.'">Click Here</a><br>
    <b>Player:</b>  '.$player.'</p></td>
<td width="50%"><p>        
    <b>Color:</b>  '.$color.'<br>
    <b>Genetics: </b>'.$genetics.'<br>
    <b>Markings:</b>  '.$markings.'<br>
    <b>Traits:</b>  '.$traits.'<br>
    <b>Defects:</b>  '.$defects.'<br>
    <b>Extras:</b>  '.$extras.'</p></td>
    </table><br>

    ';






++$i; }} else { $row = '<tr><td colspan="2" align="center">Database empty.</td></tr>'; }

mysql_close();
?>

<? echo $row ?>
</p><center>
<font face="tahoma" style="font-size: 12px;">Pages: 
<?php
include("connect.php");
$result = mysql_query("SELECT COUNT(name) FROM characters");
$row = mysql_fetch_row($result);
$total_records = $row[0];
$total_pages = ceil($total_records / 20);

for ($i=1; $i<=$total_pages; $i++) {
        echo "<a href='index.php?page=".$i."'>".$i."</a> | ";
};
?>
</center></font> 
</body>
</html>

2 个答案:

答案 0 :(得分:0)

SELECT COUNT(id) FROM characters where gender = 'female'

这将为您提供具有“女性”作为性别价值的角色数量。这就是你想要的吗?

答案 1 :(得分:0)

以下是您可以编辑此内容的示例

<?php
$cnt = 0;
$query = "select * from table";
$resulr = mysql_query($query);
while($var = mysql_fetch_array($result)){
  if($var['gender']=='female'){
    $cnt++;
  }
}
echo "We have ".$cnt." female characters";
?>