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

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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