表PHP中的echo发生次数 [英] Count occurrence in table PHP echo

查看:97
本文介绍了表PHP中的echo发生次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是PHP和MySQL的新手&虽然在StackOverflow中有很多这样的例子,但是没有一个适用于我的情况。



所以,我有一个表(名为

  student_name |  student_id |老师
----------------------------------------
乔| 991991991 |史密斯先生
Sally | 717356152 | Lozano女士
Benny | 383717747 |约翰逊先生
Jim | 191918918 |史密斯先生
John | 182783718 |史密斯先生
Alfred | 374878372 |约翰逊先生

我想要一个 HTML / PHP 页面将其输出到我的DIV中,自动遍历教师的所有选项并计算每位教师出现的次数。我希望它可以在添加新教师时自动计算新教师数量,这样我就不用在后面更改代码。

  Mr 。 Smith:3 
Johnson先生:2
Lozano女士:1

它不需要以任何顺序,我不应该把它们从最小到最大。



我试过做类似于



 <?php SELECT teacher,SUM(1)FROM votes GROUP BY teacher?> 

  SELECT teacher,count(teacher)
FROM votes
GROUP by teacher

但它们不起作用,给我一个错误语法错误,意外的'老师'(T_STRING)
我可能正在执行它们错误。他们只是在自己的发生,或者他们需要一个自己的连接或他们自己的声明或之前的东西吗?



有什么建议吗?



编辑:以下是我的全身代码与我的连线...

  $ connection = mysql_connect($ serverName,$ userName,$ password)或die('无法连接到数据库主机'。mysql_error()); 
$ dbselect = mysql_select_db($ dbname,$ connection)或者die(无法选择数据库:$ dbname。mysql_error());
?>
< / head>
< body>
< div align =center>
< form method =postaction =vote_1.php>
< h1>计数投票总数< / h1>
< h4>(这些值是当前值)< / h4>
< h2> Cantidate的投票< / h2>
<?php SELECT teacher,count(teacher)
FROM votes
GROUP by teacher?>
< / br>< / br>< / br>
< / form>
< / body>

因为密码等原因,我没有包括我的头像。



 <?php SELECT teacher,count(teacher)
FROM votes
GROUP by teacher?>

就PHP而言,这是纯文字&不是MySQL查询。快速走出我的头脑,这应该向您展示PHP& MySQL相互协作。请注意 mysql_query

  $ connection = mysql_connect($ serverName,$用户名,$密码)或死亡('无法连接到数据库主机'。mysql_error()); 
$ dbselect = mysql_select_db($ dbname,$ connection)或者die(无法选择数据库:$ dbname。mysql_error());
$ result = mysql_query(SELECT teacher,count(teacher)as teacher_count FROM votes GROUP by teacher;);

while($ row = mysql_fetch_assoc($ result)){
echo $ row ['teacher'];
echo $ row ['teacher_count'];

也就是说, mysql _ * 带前缀的PHP函数被折旧。这意味着他们将不再在即将到来的PHP版本中工作。所以这里是你的代码版本,使用 mysqli _ * 代替

  $ link = mysqli_connect($ serverName,$ rel =nofollow>使用官方PHP文档中的示例) $ userName,$ password,$ dbname); 
$ b $ //检查连接
if(mysqli_connect_errno()){
printf(Connect failed:%s\\\
,mysqli_connect_error());
exit();
}

//选择查询返回结果集
if($ result = mysqli_query($ link,SELECT teacher,count(teacher)as teacher_count FROM teacher GROUP by teacher; )){
//这是可选的。随意评论这一行。
printf(Select returned%d rows.\\\
,mysqli_num_rows($ result));

//循环遍历结果
while($ row = $ result-> fetch_object()){
echo $ row ['teacher'];
echo $ row ['teacher_count'];
}
}


I’m new to PHP and MySQL & while there are a bunch of examples on this throughout StackOverflow, but none of them apply very well to my situation.

So, I have a table (named votes) that looks like this:

 student_name | student_id | teacher
----------------------------------------
Joe           | 991991991  | Mr. Smith
Sally         | 717356152  | Ms. Lozano
Benny         | 383717747  | Mr. Johnson
Jim           | 191918918  | Mr. Smith
John          | 182783718  | Mr. Smith
Alfred        | 374878372  | Mr. Johnson

I want an HTML/PHP page to output this in one of my DIVs, automatically looping through all options for teacher and counting how many times each teacher occurs. I want it to automatically count new teachers as they are added, so that I do not have to change the code afterwords.

Mr. Smith: 3
Mr. Johnson: 2
Ms. Lozano: 1

It does not need to be in any order, i shouldn't have put them from least to greatest.

I've tried to do something like

<?php SELECT teacher, SUM(1) FROM votes GROUP BY teacher ?>

or

SELECT teacher, count(teacher) 
  FROM votes 
 GROUP by teacher

But they don't work and give me an error of "syntax error, unexpected 'teacher' (T_STRING)" I could just be implementing them wrong. Do they just occur in their own , or do they need a their own connection or their own while statement or something before?

Any suggestions?

EDIT: Here is my full body code with my connection...

$connection = mysql_connect($serverName, $userName, $password) or die('Unable to connect to Database host' . mysql_error());
$dbselect = mysql_select_db($dbname, $connection) or die("Unable to select database:$dbname" . mysql_error());
?>
</head>
    <body>
    <div align="center">
    <form method="post" action="vote_1.php">
    <h1>Counted Vote Totals</h1>
    <h4>(these values are current)</h4>
    <h2>Votes by Cantidate</h2>
    <?php SELECT teacher, count(teacher) 
  FROM votes 
 GROUP by teacher ?>
</br></br></br>
</form>
</body>

I did not include my head because of passwords, etc.

解决方案

What exactly is this here:

<?php SELECT teacher, count(teacher) 
  FROM votes 
 GROUP by teacher ?>

As far as PHP is concerned, that is plain text & not a MySQL query. Quickly off the top of my head, this should work to show you the basic concept of how PHP & MySQL work with each other. Note the mysql_query.

$connection = mysql_connect($serverName, $userName, $password) or die('Unable to connect to Database host' . mysql_error());
$dbselect = mysql_select_db($dbname, $connection) or die("Unable to select database:$dbname" . mysql_error());
$result = mysql_query("SELECT teacher, count(teacher) as teacher_count FROM votes GROUP by teacher;");

while ($row = mysql_fetch_assoc($result)) {
    echo $row['teacher'];
    echo $row['teacher_count'];
}

That said, mysql_* prefixed PHP functions are depreciated. Meaning they will no longer work in upcoming version of PHP. So here is a version of your code using mysqli_* instead using examples from the official PHP documentation:

$link = mysqli_connect($serverName, $userName, $password, $dbname);

// Check the connection
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

// Select queries return a resultset
if ($result = mysqli_query($link, "SELECT teacher, count(teacher) as teacher_count FROM votes GROUP by teacher;")) {
    // This is optional. Feel free to comment out this line.
    printf("Select returned %d rows.\n", mysqli_num_rows($result));

    // Cycle through results
    while ($row = $result->fetch_object()){
        echo $row['teacher'];
        echo $row['teacher_count'];
    }
}

这篇关于表PHP中的echo发生次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆