0

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

Edited by extemer: n/a

3
Contributors
12
Replies
13
Views
6 Years
Discussion Span
Last Post by jonnysmiths
0
$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);
0

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

0

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>';

0

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..

0

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/>';
}

Edited by hielo: n/a

0

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>";

Edited by extemer: n/a

0

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>';
0

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>';


?>
0

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.

0

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...

0

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

0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.