Click to See Complete Forum and Search --> : db Function help


cwilkey
01-30-2007, 07:41 PM
Hi All,

I'm writing a function to dynamiclly generate an SQL statment. The function works fine however it is only return the first row. How can I get it to bring back all results?

Here is my function:

function getNews($criteria, $order, $limit) {

$sql = "SELECT * FROM articles WHERE category1 = '$criteria' AND CURDATE() BETWEEN startdate AND enddate AND active = 'Y' ORDER BY `$order` LIMIT $limit";
$call = mysql_query($sql) or die(mysql_error());
$results = mysql_fetch_assoc($call);
return $results;
}


Here is how I'm calling it:
[code]
<?php $news = getNews('Top Story', 'timestamp', '3') while ($results = mysql_fetch_assoc($call)) { ?>

do blah, blah

<?php } ?>

Sheldon
01-30-2007, 08:46 PM
i think maybe more like


<?php

function getNews($criteria, $order, $limit) {

$sql = "SELECT * FROM articles WHERE category1 = '$criteria' AND CURDATE() BETWEEN startdate AND enddate AND active = 'Y' ORDER BY `$order` LIMIT $limit";
$call = mysql_query($sql) or die(mysql_error());

return $call;
}

$news = getNews('Top Story', 'timestamp', '3');
while ($results = mysql_fetch_assoc($news)) {

echo($results['database_row']);

}
?>

cwilkey
01-30-2007, 08:51 PM
YES!!! That did the trick. Thank you! Thank you!

Sheldon
01-30-2007, 09:06 PM
no worries