计算一个数据库字段中的项目数
我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
检查行数:
PHP 手册 (mysql_num_rows)
或者首先
SELECT COUNT(*)
。编辑
哦,没关系,看起来您正在将逗号分隔的列表存储在单个单元格中。顺便说一句,这是一个不好的做法。您可能只需要另一个 MySQL 表来正确存储它们。如果您决定不修复数据库架构,尽管这应该适合您:
PHP 手册 (爆炸)
Check the number of rows:
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:
PHP Manual (explode)
尝试
Try
感谢@PaulPRO,我找到了这个解决方案,
我不确定它是否正确,请随时纠正我
Thanks to @PaulPRO I came to this solving solution
I'm not sure if it's correct, feel free to correct me