我有一个MySQL表,它有一个数组列,而每个元素都用“,”分隔。我试着得到所有行的总数(145)
当我运行以下代码时,我得到了145个$ index,这是正确的但$ totalClasses是0.所以显然我错过了一些东西。
如果有人会以我的方式指出错误,我将不胜感激!
谢谢,
维克
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
答案 0 :(得分:0)
IN
$SQL="SELECT ExhibitorClasses FROM tblShowEntries WHERE ShowID = $ShowID AND ExhibitorClasses IS NOT NULL AND ExhibitorClasses !=' '";
你没有连接var $ ShowID正确的是:
$SQL="SELECT ExhibitorClasses FROM tblShowEntries WHERE ShowID = ".$ShowID." AND ExhibitorClasses IS NOT NULL AND ExhibitorClasses !=' '";
答案 1 :(得分:0)
感谢所有回复的人 - 这是我的最终代码。可能有一种更好的方法可以做到这一点,但它确实有效,所以猜对了。
$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;