Page 1 of 1

Counter problem!

Posted: Mon Jan 25, 2010 1:29 pm
by cap2cap10
Hey php technorati. I have another problem. I am trying to create a counter that conforms to certain values that are present in the fields of a mysql db. here is my code:

Code: Select all

$query = "SELECT month, pay_type FROM js_login WHERE agentID='$_SESSION['agent_login']['agentID']' AND month= 'January' And pay_type= 'p1'";
$result = mysql_query($query) or die(mysql_error());
 
$count = '0';
while($row = mysql_fetch_assoc($result))
{
    $count ++;
}
 
echo "$count";
 
mysql_close();
 
?>
I need it to count every row that has those specific variables.But I am getting a error:

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING

Please advise captain!!!! :banghead:

Thanks in advance,

Batoe

Re: Counter problem!

Posted: Mon Jan 25, 2010 1:58 pm
by AbraCadaver

Code: Select all

$count = mysql_num_rows($result);

Re: Counter problem!

Posted: Mon Jan 25, 2010 2:05 pm
by feden
Check the quotes!

Code: Select all

$query = "SELECT month, pay_type FROM js_login WHERE agentID='" . $_SESSION['agent_login']['agentID'] . "' AND month= 'January' And pay_type= 'p1'";
However, I think there is a better way to do it:

Code: Select all

 
$query = "SELECT COUNT(*) AS num FROM js_login WHERE agentID='" . $_SESSION['agent_login']['agentID'] . "' AND month= 'January' And pay_type= 'p1'";
 
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_assoc($result);
 
echo $row['num'];
 
Bye! :wink: