/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] while() problem

at least i think thats what the problem is

[code=php]
<?php
$pieces = mysql_query(“SELECT * FROM `pieces` ORDER BY `date_submitted` DESC”);
?>

Newest Pieces:

<?php
$i = 0;
while($piece = mysql_fetch_array($pieces, MYSQL_ASSOC) && $i <= 5){
echo mysql_error();
?>

<a href=”view.php?id=<?php echo $piece[‘id’] ?>”><?php echo $piece[‘piecename’] ?></a> by <?php echo $piece[‘artist’] ?>
<br />

<?php
$i++;
}
?>
[/code]

i get the line breaks and the empty links, so basically its not getting the stuff from the db in there. ran mysql_error, nothing. maybe while doesnt want me to stick something else in there with the mysql_fetch_Array? if so, any suggestions for another way to do this? all i want to do is get the 5 newest pieces in descending order of newness. is there a way that i can even put in the SQL statement “only grab a max of 5 rows”

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@balloonbuffoonMar 30.2006 — Try this to debug and see if anything at all is in the $piece array:
[code=php]<?php
$pieces = mysql_query("SELECT * FROM pieces ORDER BY date_submitted DESC");

echo "<pre>";
$i = 0;
while($piece = mysql_fetch_array($pieces, MYSQL_ASSOC) && $i <= 5){
print_r($piece);
$i++;
}
echo "</pre>";
?> [/code]
Copy linkTweet thisAlerts:
@bathurst_guyMar 30.2006 — [code=php]<?php
$pieces = mysql_query("SELECT * FROM pieces ORDER BY date_submitted DESC LIMIT 0, 5");
echo "Newest Pieces:";
$num = mysql_num_rows($pieces);
for($i=0; $i <= $num;$i++){
$piece = mysql_fetch_array($pieces);
echo '<a href="view.php?id='.$piece['id'].'">'.$piece['piecename'].'</a> by '.$piece['artist'].'<br />';
}
?> [/code]
Copy linkTweet thisAlerts:
@gameguy43authorMar 30.2006 — working code:
[code=php]
<?php

$pieces = mysql_query("SELECT * FROM pieces ORDER BY date_submitted DESC LIMIT 0, 5");
?>

Newest Pieces:

<?php
$i = 0;
while($piece = mysql_fetch_array($pieces, MYSQL_ASSOC)){
?>

<a href="view.php?id=<?php echo $piece['id'] ?>"><?php echo $piece['piecename'] ?></a> by <?php echo $piece['artist'] ?>
<br />

<?php } ?>
[/code]

must've been something about formatting the while like that. and i did try the print_r thing to debug, it printed "1" each time. thats what led me to think there was a problem with the while(), great minds htink alike. anyway, problems solved, thanks guys.
×

Success!

Help @gameguy43 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.13,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...