Forum Moderators: coopster

Message Too Old, No Replies

count rows between certain dates with php code?

count rows between certain dates with php code?

         

shams

12:55 am on May 19, 2006 (gmt 0)

10+ Year Member



hi,
this php script should count the rows between certain dates but it is not working the output is:
There are total TB patients
can't count the rows this is the code:
<?php
$dateReg1 = $_POST['dateReg1'];
$dateReg2 = $_POST['dateReg2'];
$dateAttSr1 = $_POST['dateAttSr1'];
$dateAttSr2 = $_POST['dateAttSr2'];
$dateAttSt1 = $_POST['dateAttSt1'];
$dateAttSt2 = $_POST['dateAttSt2'];
$link = mysql_connect("localhost", "root");
mysql_select_db("mydb");

$result = mysql_query("SELECT * FROM register WHERE dateReg >= '$dateReg1' and dateReg <= '$dateReg2' or dateAttSr1 >= '$dateAttSr1' and dateAttSr2 <= '$dateAttSr2' or dateAttSt1 >= '$dateAttSt1' and dateAttSt2 <= '$dateAttSt2' ");
$num_rows = mysql_num_rows($result);

echo "There are total"." ".$num_rows." "."TB patients\n";
?>

eelixduppy

3:08 am on May 19, 2006 (gmt 0)



Try changing the query to:
$result = mysql_query("SELECT * FROM register WHERE (dateReg >= '$dateReg1' and dateReg <= '$dateReg2') or (dateAttSr1 >= '$dateAttSr1' and dateAttSr2 <= '$dateAttSr2') or (dateAttSt1 >= '$dateAttSt1' and dateAttSt2 <= '$dateAttSt2')");

dreamcatcher

7:00 am on May 19, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi shams,

Add some debugging to your query:


$result = mysql_query("SELECT * FROM register WHERE dateReg >= '$dateReg1' and dateReg <= '$dateReg2' or dateAttSr1 >= '$dateAttSr1' and dateAttSr2 <= '$dateAttSr2' or dateAttSt1 >= '$dateAttSt1' and dateAttSt2 <= '$dateAttSt2' ")or die(mysql_error());

Also, you can use the BETWEEN operator.


$result = mysql_query("SELECT * FROM register WHERE (dateReg BETWEEN '$dateReg1' AND '$dateReg2') OR (dateAttSr1 BETWEEN '$dateAttSr1' AND '$dateAttSr2') OR (dateAttSt1 BETWEEN '$dateAttSt1' AND '$dateAttSt2')") or die(mysql_error());

dc

shams

11:25 am on May 19, 2006 (gmt 0)

10+ Year Member



thanks for replies both queries are working perfect the debugg info done the work.