I'm just beginning in PHP. I have a problem and hope someone can help. In the code below, if the SELECT query does not contain && assn ='$value', then everything works. I am trying to run the query for every $value in the array and each time determine the number of $rows for each query result. $rows is added to $total each time to keep a running count of number of rows.
When I run the code below, the ECHO runs for as many times as the FOREACH runs, and $total always is zero. I suspect I have an error in the SELECT statement. It may be the syntax for including a variable that contains an element from an array.
I've checked to make sure the array is valid by echoing and printing the $key and $value and the data is correctly displayed. The database has records that meet the query criteria so the query should produce results. The query does work as well as the rest of the code if the query is run without && assn ='$value'.
Thanks in advance.
Code:
<?php
$dbc = mysql_connect('xxxx', 'yyyy','xxxx')
or die('Error connecting to MySQL server.');
mysql_select_db('mydb');
// Number of closed condo sales for 7-12 months prior to effective date
$total = 0;
foreach ($_POST['condo'] as $key => $value){
$results = mysql_query("select * from compsin where cldate > '$effdate365' && cldate < '$effdate182' && assn ='$value' ");
$rows = mysql_num_rows($results);
if ($rows < 1)
{
echo "No Matching records.<br>";
}
else
{
$total = ($rows + $total);
}
}
?>
|