计算一个数据库字段中的项目数

发布于 2024-12-01 17:38:33 字数 1950 浏览 0 评论 0原文

我的 MySQL 数据库中有一个名为 Sponsors 的表。
该表有两个字段。 事件名称。字段names 填充有不同的单词。例如Pieter、Sam、Thomas。我想计算 names 中的名称数量。

我如何使用 PHP 和 SQL 请求来做到这一点。我虽然是这样的......

$query = mysql_result(mysql_query("SELECT names FROM Sponsors WHERE id = '55'"));
$result = str_replace(',',' ', $query);
$total = count($result);

但这并没有解决它......

编辑

if(!empty($activiteiten))
    {
        foreach($activiteiten as $k => $v) 
            {
                $query = mysql_result(mysql_query('SELECT aanwezig FROM tblLeidingAgenda WHERE id = "'.$v["id"].'"),0');
                $result = str_replace(", ", " ", $query);
                $total = str_word_count($result);

                echo '<div class="lof-main-item">';
                echo '<img src="images/791902news3.jpg" title="Newsflash 2" height="300" width="900">           
                            <div class="lof-main-item-desc">
                                <h3><a target="_parent" title="Newsflash 2" href="#">'.$v["uur"].'</a></h3>
                        <p>'.$v["titel"].'</p>
                        ID: '.$v["id"].'
                    <form method="post" action="#">
                        <input type="submit" name="aanwezig" value="Ik ben aanwezig"/><br />
                        <input type="submit" name="afwezig" value="Ik ben afwezig"/><br />
                        <input type="submit" name="herinnering" value="Stuur herinnering"/><br />
                        <input type="text" name="id" value="'.$v["id"].'" />
                        Ik ben aanwezig ('.$total.'): '.$v["aanwezig"].'<br />
                        Ik ben afwezig: '.$v["afwezig"].'</form></div></div> ';
            }
    }

I have a table in my MySQL database called Sponsors.
This table has a two field. event and names. The field names is filled with different words. For example Pieter, Sam, Thomas. I would like to count the number of names in names.

How can I do this using PHP and SQL-requests. I though something like this...

$query = mysql_result(mysql_query("SELECT names FROM Sponsors WHERE id = '55'"));
$result = str_replace(',',' ', $query);
$total = count($result);

But this didn't solve it...

EDIT

if(!empty($activiteiten))
    {
        foreach($activiteiten as $k => $v) 
            {
                $query = mysql_result(mysql_query('SELECT aanwezig FROM tblLeidingAgenda WHERE id = "'.$v["id"].'"),0');
                $result = str_replace(", ", " ", $query);
                $total = str_word_count($result);

                echo '<div class="lof-main-item">';
                echo '<img src="images/791902news3.jpg" title="Newsflash 2" height="300" width="900">           
                            <div class="lof-main-item-desc">
                                <h3><a target="_parent" title="Newsflash 2" href="#">'.$v["uur"].'</a></h3>
                        <p>'.$v["titel"].'</p>
                        ID: '.$v["id"].'
                    <form method="post" action="#">
                        <input type="submit" name="aanwezig" value="Ik ben aanwezig"/><br />
                        <input type="submit" name="afwezig" value="Ik ben afwezig"/><br />
                        <input type="submit" name="herinnering" value="Stuur herinnering"/><br />
                        <input type="text" name="id" value="'.$v["id"].'" />
                        Ik ben aanwezig ('.$total.'): '.$v["aanwezig"].'<br />
                        Ik ben afwezig: '.$v["afwezig"].'</form></div></div> ';
            }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

七堇年 2024-12-08 17:38:33

检查行数:

$total = mysql_num_rows($result);

PHP 手册 (mysql_num_rows)

或者首先SELECT COUNT(*)

编辑

哦,没关系,看起来您正在将逗号分隔的列表存储在单个单元格中。顺便说一句,这是一个不好的做法。您可能只需要另一个 MySQL 表来正确存储它们。如果您决定不修复数据库架构,尽管这应该适合您:

$query = mysql_result(mysql_query("SELECT names FROM Sponsors WHERE id = '55'"));
$result = explode(',',$query);
$total = count($result);

PHP 手册 (爆炸)

Check the number of rows:

$total = mysql_num_rows($result);

PHP Manual (mysql_num_rows)

Or SELECT COUNT(*) in the first place.

Edit

Oh nevermind, it looks like you're storing a comma separated list right in a single cell. That's a bad practice btw. You probably just need another MySQL table to store them properly. If you decide not to fix database schemat though this should work for you:

$query = mysql_result(mysql_query("SELECT names FROM Sponsors WHERE id = '55'"));
$result = explode(',',$query);
$total = count($result);

PHP Manual (explode)

时光瘦了 2024-12-08 17:38:33

尝试

$query = mysql_result(mysql_query("SELECT names FROM tblLeidingAgenda WHERE id = '55'"), 0, 'names');
$result = explode(',' $query);
$total = count($result);

Try

$query = mysql_result(mysql_query("SELECT names FROM tblLeidingAgenda WHERE id = '55'"), 0, 'names');
$result = explode(',' $query);
$total = count($result);
寂寞陪衬 2024-12-08 17:38:33

感谢@PaulPRO,我找到了这个解决方案,

$query = mysql_result(mysql_query("SELECT afwezig FROM tblLeidingAgenda WHERE id = '55'"),0);
$result = str_replace(", ", " ", $query);
$total = str_word_count($result);

我不确定它是否正确,请随时纠正我

Thanks to @PaulPRO I came to this solving solution

$query = mysql_result(mysql_query("SELECT afwezig FROM tblLeidingAgenda WHERE id = '55'"),0);
$result = str_replace(", ", " ", $query);
$total = str_word_count($result);

I'm not sure if it's correct, feel free to correct me

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文