here is my code want to get records between two dates

$sql=mysql_query("SELECT * FROM `expense` WHERE `date` BETWEEN '2011-04-26' AND '2011-04-28'")
 
 or die(mysql_error());

echo $r=mysql_fetch_assoc($sql);

i getting "Array" in output instead of the data please sort it out for me i am not getting it right

Recommended Answers

All 12 Replies

$sql=mysql_query("SELECT * FROM `expense` WHERE `date` BETWEEN '2011-04-26' AND '2011-04-28'") or die(mysql_error());

$r=mysql_fetch_assoc($sql);

//SELECT * ... assuming that the * includes some field named `id`, then the following
//will print it's contents:
echo $r['id'];


//if you want to see everything at once, use:
print_r($r);

when wrote this code

$sql=mysql_query("SELECT * FROM `expense` WHERE `date` BETWEEN '2011-04-26' AND '2011-04-28'")
 
 or die(mysql_error());

$r=mysql_fetch_assoc($sql);
print_r($r);

i got this output
Array ( [rec_no] => 5 [date] => 2011-04-26 [detail] => dddd [amount] => 111 )

why i am getting this array print again and again cant i get it without array

why i am getting this array

Because that's what print_r() does.

can't i get it without array

echo/print them individually - ex: echo $r['rec_no'] . ' ' . $r['date'].'<br>'; Or for all of them (without Array): echo implode(' ', $r).'<br>';

thanks anyways but i am not getting the full records between date 26th to 29th it just display the first mention date e.g

WHERE `date` BETWEEN '2011-04-26' AND '2011-04-29'

it only print me one record that of date '2011-04-26' but i want it to print all records in between date 26 till to 29..

you need to iterate over the entire result set:

...
$sql=mysql_query("SELECT * FROM `expense` WHERE `date` BETWEEN '2011-04-26' AND '2011-04-28'")
 
 or die(mysql_error());

while( $r=mysql_fetch_assoc($sql) )
 echo implode(' ',$r).'<br/>';
}

brother i have come out with this way and this works

$sql=mysql_query("SELECT * FROM `expense` WHERE `date` BETWEEN '2011-04-26' AND '2011-04-29'");


echo "<center><table bordercolor='#000000' border='3' >";

while( $r=mysql_fetch_assoc($sql) )
{
	
	$new=implode(' ',$r).'<br/>';
echo "</tr><td>$new</td>";

}

echo "</tr></table></center>";

the table markup you are emitting is not correct. Also, there's no need to save it onto $new .
Try:

$sql=mysql_query("SELECT * FROM `expense` WHERE `date` BETWEEN '2011-04-26' AND '2011-04-29'");


echo '<center><table bordercolor="#000000" border="3" >';

while( $r=mysql_fetch_assoc($sql) )
{
	
  echo '<tr><td>' . implode(' ',$r) . '</td></tr>';

}

echo '</table></center>';

this will display me all the amount field from 26th date till 29th and if i want to add that all amount display how would i perform that..i would use sum function instead the while loop

<?php
error_reporting(0);
mysql_connect("localhost","root",""); 
mysql_select_db("new"); // connect to database


 $sql=mysql_query("SELECT * FROM `expense` WHERE `date` BETWEEN '2011-04-26' AND '2011-04-29'");
  


echo '<center><table bordercolor="#000000" border="3" >';
//
while( $r=mysql_fetch_assoc($sql) )
while($r1=mysql_fetch_assoc($new)){
	echo $r1['amount'];
{


echo '<tr><td>' . $r['amount'] .'<br>'. '</td></tr>';
 

}
}
echo '</table></center>';


?>

this will display me all the amount field from 26th date till 29th and if i want to add that all amount display how would i perform that..i would use sum function instead the while loop

if you want every record AND the sum, then initialize a $total=0; variable outside the while. Then within the while put $total+=$r['amount']; . After the while, $total should have the sum.

If all you want/need is the sum, then do a SUM query.

iwant to to knwn that if a student has not paid fee on the month and next month he pay how would a query will be used to double his fee becuase he has not pay for previous month also..should i use while loop...

isort out a way that i should use while loop for it and after every 30days the value should be doubles but what would happen in the case of months having less than 30days..i cant sort out the way

You check the error by expert teacher or good software engginer they need help you,they solve your quetion.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.