Jump to content

Do I need a parameter in this function?


unemployment

Recommended Posts

function fetch_feedback_best()
{
$sql = 	"SELECT
			`like`
		FROM `feedback`
		GROUP BY `page` asc 
		LIMIT 10";

while($row = mysql_fetch_assoc($sql))
{
	$results[] = $row;
}

return $results;
}

 

This doesn't work.  My error is "Warning: mysql_fetch_assoc() expects parameter 1 to be resource, string given in /blah.inc.php on line 105 ".

 

<?php

$results = fetch_feedback_best();

foreach ($results as $result)
{
	?>
	 echo $result['page'];		
}

?>

Link to comment
https://forums.phpfreaks.com/topic/230007-do-i-need-a-parameter-in-this-function/
Share on other sites

You're never performing the query, you're only defining it. Try:

<?php
function fetch_feedback_best()
{
$sql = 	"SELECT
			`like`
		FROM `feedback`
		GROUP BY `page` asc 
		LIMIT 10";
$rs = mysql_query($sql);
while($row = mysql_fetch_assoc($rs))
{
	$results[] = $row;
}

return $results;
}
?>

 

Ken

Thank you... That worked, but I have another question.

 

In MySQL I have a table with two columns.

 

page              likes

 

profile              1

profile              1

index                1

company          1

company          1

company          1

company          1

 

I want to display the top ten best performing pages in order with my group by...  It's not display correctly though

 

function fetch_feedback_top_performers()
{
$sql = 	"SELECT
			`page`,
		COUNT(`like`)
		FROM `feedback`
		GROUP BY `page` 
		ORDER BY `like` asc 
		LIMIT 10";

$rs = mysql_query($sql);

while($row = mysql_fetch_assoc($rs))
{
	$results[] = $row;
}

return $results;
}

 

So essentially, this should read

 

Company (4)

Profile (2)

Index (1)

Your ORDER BY is ascending, so you are getting the least liked pages first. Change the ORDER BY to DESCending to get the most liked pages first.

 

	$sql = 	"SELECT
			`page`,
		COUNT(`like`)
		FROM `feedback`
		GROUP BY `page` 
		ORDER BY `like` DESC
		LIMIT 10";

Oops, good catch sasa. Those backticks always screw with my head. I usually just alias the expression and order by the alias (and I never use backticks):

 

SELECT page, COUNT(like) AS Cnt
FROM feedback
GROUP BY page 
ORDER BY Cnt DESC
LIMIT 10;

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.