Jump to content

[SOLVED] Select records by months


yandoo

Recommended Posts

Hi there,

 

Im writing a little php project to store vegetables and various attributes of them in a database.

 

I have 2 tables; Vegetable table and Sow table.

 

Vegetable table

VegeID

Name

Etc

 

Sow table

SowID

VegeID*

Jan

Feb

Mar

Apr

Etc

 

The Sow table contains the months jan to dec and is either a 1 or a 0 if that vegetable can be sown on that month.

 

I want to SELECT only the vegetables from the Vegetable Table in the database (as they contain more details)that CAN be sown in the current month (or perhaps by user inputted month?).

 

im just not sure where to start with this one and would really appreciate any help you wonderful people could offer.

 

Thank You  :D

Link to comment
https://forums.phpfreaks.com/topic/172299-solved-select-records-by-months/
Share on other sites

Hi there,

 

Ive followed your method and adapted it slightly to:

 

<?php $month = 'Jul';
mysql_select_db($database_connect, $connect);
$query_sql = 'select V.VegeID,V.Name from vegetable V,outdoorsowtime S where S.'.$month.' = 1 and V.VegeID = S.VegeID';
$sql = mysql_query($query_sql, $connect) or die(mysql_error());
$row_sql = mysql_fetch_assoc($query_sql);
echo $sql; 
// echo $query_sql;
mysql_free_result($sql);
?>

 

It comming up with an error:

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, string given in E:\wamp\www\greenlife\view_vege.php on line 13

Resource id #4

 

Any ideas what im missing here?

 

Thank You :)

 

Just realised:  $row_sql = mysql_fetch_assoc($query_sql); should be : $row_sql = mysql_fetch_assoc($sql);

 

Still get the error: Resource id#4

I alos realise i need a While statement in there and the code is now:

 

 <?php $month = 'Jul';
mysql_select_db($database_connect, $connect);
$query_sql = 'select V.VegeID,V.Name from vegetable V,outdoorsowtime S where S.'.$month.' = 1 and V.VegeID = S.VegeID';
$sql = mysql_query($query_sql, $connect) or die(mysql_error());

while ($row = mysql_fetch_assoc($sql)) {
    echo $row['Name']."<br />\n";
echo $sql;
}


mysql_free_result($sql);
?>

 

I think its working but only halve 1 recordi n db so gona add and test...

 

Thanks

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.