Forum Moderators: coopster

Message Too Old, No Replies

mysql_fetch_array( ) help

         

Sarah Atkinson

9:27 pm on May 18, 2005 (gmt 0)

10+ Year Member



How do you fetch one row at a time? Like say I want to get just the rows 5-10 that it returns.

StupidScript

9:29 pm on May 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



select * from table where condition limit 1

or

limit 5,5
to get #5-#10.

dreamcatcher

7:40 am on May 19, 2005 (gmt 0)

killroy

9:29 am on May 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You might also find mysql_fetch_row() useful:
[php.net...]

SN

Sarah Atkinson

4:32 pm on May 19, 2005 (gmt 0)

10+ Year Member



The thing I wanted was select 30 entries but then I wanted one php script to handle the first 5 another to handle the second 5 and so on.
I came up with something like this.

<?php
include 'db.inc.php';
$x ='2005-05-01';
$z ='2005-05-30';
$result = @mysql_query("SELECT id, maincourse, veggie, veggie2, fruit, bread, dessert, drink, DATE_FORMAT(day, '%a %b, %D') as date_string FROM meal_menus_bbjj WHERE day>='$x' AND day<='$z' ORDER BY day");
if (!$result) {
exit('<p>Error performing query: ' . mysql_error() . '</p>');
}
echo date(w,mktime(0,0,0,2,20,2005));
for($i=0;$i<5;++$i){
$row = mysql_fetch_array($result);
$dbdate=$row["date_string"];
echo $dbdate . ' red<br>';
}
echo '<p>';
for($i=0;$i<5;++$i){
$row = mysql_fetch_array($result);
$dbdate=$row["date_string"];
echo $dbdate . ' blue<br>';
}
echo'</p>';
?>

here is the output:

0Mon May, 2nd red
Tue May, 3rd red
Wed May, 4th red
Thu May, 5th red
Fri May, 6th red

Mon May, 9th blue
Tue May, 10th blue
Wed May, 11th blue
Thu May, 12th blue
Mon May, 16th blue

ohh no someting isn't right. There is no DB entry for Friday so that won't work.

So then I though aboult having an "if" check that the next day realy is the next day and if it isn't then insert a blank. but then it would increment past that one so it wouldn't get printed where it is supose to go.

Is there a way to keep the mysql_fetch_array() from incrementing, or step it back?

Another idea I had was to add a function before it starts that checks the db to make sure all mon-fri have entries (there shouldn't be a blank day) then return an error if a day is missing.

Now if I do that will I have to have 2 query or can I reset the mysql_fetch_array()?

Sarah

StupidScript

11:19 pm on May 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hang on ... fixing ...

StupidScript

11:42 pm on May 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Luckily there are only five days involved ...!

[b]$week=array("Mon","Tue","Wed","Thu","Fri");[/b]

for($i=0;$i<5;$i++){ 

$row = mysql_fetch_array($result); 

$dbdate=$row["date_string"]; 

$tday=substr($dbdate,0,3);

if ($tday==$week[$i]) { $showday=1; }

else {

if ($i<5) { $i++; }

if ($tday==$week[$i]) { $showday=1; }

else {

if ($i<5) { $i++; }

if ($tday==$week[$i]) { $showday=1; }

else {

if ($i<5) { $i++; }

if ($tday==$week[$i]) { $showday=1; }

else {

if ($i<5) { $i++; }

if ($tday==$week[$i]) { $showday=1; }

}

}

}

}

if ($showday) {

echo $dbdate . ' red<br>'; 

}

}

IMHO it is a great idea to have entries for every day, even if they are just the day/date with no meal for that day rather than leaving gaps. But if you have to have gaps, at least the above should get your weeks separated ... no matter if there are 5 or 1 day in the db for that week.

StupidScript

11:50 pm on May 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Might be handy:

...

if ($tday==$week[$i]) { $showday=1; } 

else { 

echo "No meal for ".$week[$i]."<br />";

if ($i<5) { $i++; } 

if ($tday==$week[$i]) { $showday=1; } 

...

Sarah Atkinson

7:41 pm on May 23, 2005 (gmt 0)

10+ Year Member




Is there a way to keep the mysql_fetch_array() from incrementing, or step it back?

Another idea I had was to add a function before it starts that checks the db to make sure all mon-fri have entries (there shouldn't be a blank day) then return an error if a day is missing.

Now if I do that will I have to have 2 query or can I reset the mysql_fetch_array()?

I found this function helpfull with resetting the query array

mysql_data_seek($result, 0);