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> ';
}
}
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)