[英]Get count of elements in a specific column of a MySQL table for each row
I have a MySQL table that has an array column whereas each element is delimited by a ",". 我有一个具有数组列的MySQL表,而每个元素都由“,”分隔。 I am try to get a total count of all rows (145)
我尝试获取所有行的总数(145)
When I run the following code I am getting 145 for $index which is correct but $totalClasses is 0. So obviously I am missing something. 当我运行以下代码时,我得到的$ index为145,这是正确的,但$ totalClasses为0。因此,显然我缺少了一些东西。
I would appreciate it if someone would point out the error in my ways! 如果有人指出我的错误,我将不胜感激!
Thanks, 谢谢,
Vic 维克
echo "<br><b>Number Classes Entered:</b>";
$SQL="SELECT ExhibitorClasses FROM tblShowEntries WHERE ShowID = $ShowID AND ExhibitorClasses IS NOT NULL AND ExhibitorClasses !=' '";
$result = mysql_query($SQL);
$myClasses = array(); // make a new array
$index = 0;
while($row = mysql_fetch_assoc($result)) // loop to get the count
{
$myClasses[$index] = $row;
$userclasses = count($myClasses[$index]);
$totalclasses = $totalclasses + $userClasses;
$index++;
}
// $userClasses = explode(",", $myClasses);
// $totalclasses = count($myClasses);
echo $index . " " . $totalclasses
IN 在
$SQL="SELECT ExhibitorClasses FROM tblShowEntries WHERE ShowID = $ShowID AND ExhibitorClasses IS NOT NULL AND ExhibitorClasses !=' '";
you not concatenate var $ShowID correct is: 您没有将var $ ShowID连接起来,正确的是:
$SQL="SELECT ExhibitorClasses FROM tblShowEntries WHERE ShowID = ".$ShowID." AND ExhibitorClasses IS NOT NULL AND ExhibitorClasses !=' '";
Thanks to all who responded - Here is my final code. 感谢所有回答-这是我的最终代码。 There may be a better way to do this but it works so guess thats ok.
也许有更好的方法可以做到这一点,但是它可以工作,所以没关系。
$SQL="SELECT ExhibitorClasses FROM tblShowEntries WHERE ShowID = ".$ShowID." AND ExhibitorClasses IS NOT NULL AND ExhibitorClasses !=' '";
$result = mysql_query($SQL);
$myClasses = array(); // make a new array to hold my classes
$totalClasses=0;
$index = 0;
$query = mysql_query($SQL);
$row = mysql_fetch_row($query);
while($row = mysql_fetch_row($query)) // loop
{
$myClasses[$index] = $row[0];
$classes = explode(",", $myClasses[$index]);
$count = 0;
foreach ($classes as $item) {
$count++;
}
$totalClasses = $totalClasses + $count;
$index++;
}
echo "<br><b>Number Classes Entered:</b> ".$totalClasses;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.