Search Webmaster Help and Solution

Switch statement with varchar?

Hiya. Here is my question:

I have a database for a member listing. I have 1 table organized as follows:

id
name
sex

The "sex" field can either be Male or Female. In the table, it is defined as sex varchar(20). I want to calculate the total number of males, and the total number of females, but I ran into problems trying to compare strings. Does PHP even support the comparison of strings?

My switch statement is set up like this:

if ($myrow = mysql_fetch_array($result)) {
do {
switch ($myrow["sex"]) {
case male:
$male++
break;
case female:
$female++;
break;
default:
echo "Error.";
}
} while ($myrow = mysql_fetch_array($result));
}

echo "Total Males = $male<br>";
echo "Total Females = $female<br>";


Yet, all it prints is "Error.Error.Error..." until the # of rows runs out, which means it is jumping to the default scenario each time. Im new to PHP (I just started playing around with it yesterday), and I thank you in advance for your help Hiya. Here is my question:

I have a database for a member listing. I have 1 table organized as follows:

id
name
sex

The "sex" field can either be Male or Female. In the table, it is defined as sex varchar(20). I want to calculate the total number of males, and the total number of females, but I ran into problems trying to compare strings. Does PHP even support the comparison of strings?

My switch statement is set up like this:

if ($myrow = mysql_fetch_array($result)) {
do {
switch ($myrow["sex"]) {
case male:
$male++
break;
case female:
$female++;
break;
default:
echo "Error.";
}
} while ($myrow = mysql_fetch_array($result));
}

echo "Total Males = $male<br>";
echo "Total Females = $female<br>";


Yet, all it prints is "Error.Error.Error..." until the # of rows runs out, which means it is jumping to the default scenario each time. Im new to PHP (I just started playing around with it yesterday), and I thank you in advance for your help

View Complete Thread with Replies

Related Items

Query failed: connection to localhost:3354 failed (errno=111, msg=Connection refused).