Page 1 of 1

Problem with max() in php :S

Posted: Sat Jul 07, 2012 11:08 am
by mekha
hi guys,
i get this error:
Warning: Wrong parameter count for max() in /home/mekhamata/domains/topline.me/public_html/poll/poll.php on line 17
and my code is:

Code: Select all


<?php
	
	$query = mysql_query("SELECT * FROM `poll` ORDER BY `id` DESC LIMIT 1");
	$rows = mysql_num_rows($query);
	
	if($rows > 0){
		$poll = mysql_fetch_array($query);
		$title = $poll['name'];
	} else {
		$title = 'No Poll Yet';
	}
	
	$query = mysql_query("SELECT COUNT(`id`) as hits FROM `responses` GROUP BY `qid`");
	while($row = mysql_fetch_array($query)){
		$me[] = $row['hits'];
	}
	$max = max($me);
	
	$query = mysql_query("SELECT `questions`.`pid` FROM  `responses`, `questions` WHERE `responses`.`qid`=`questions`.`id` AND `responses`.`ip`='".$_SERVER['REMOTE_ADDR']."' AND pid='".$poll['id']."'");
	
	if(mysql_num_rows($query) > 0){
	$total = mysql_query("SELECT `questions`.`pid` FROM  `responses`, `questions` WHERE `responses`.`qid`=`questions`.`id` AND pid='".$poll['id']."'");
	$total = mysql_num_rows($total);
?>




Re: Problem with max() in php :S

Posted: Sat Jul 07, 2012 11:22 am
by mekha
i tryed to add:

Code: Select all

$me = array();
below:

Code: Select all

$query = mysql_query("SELECT COUNT(`id`) as hits FROM `responses` GROUP BY `qid`");
like this:

Code: Select all

$query = mysql_query("SELECT COUNT(`id`) as hits FROM `responses` GROUP BY `qid`");
$me = array();
and didnt succeed

Re: Problem with max() in php :S

Posted: Sat Jul 07, 2012 4:29 pm
by Christopher
Shouldn't it be counting qid?

Code: Select all

SELECT COUNT(`qid`) as hits FROM `responses` GROUP BY `qid`");

Re: Problem with max() in php :S

Posted: Sat Jul 07, 2012 5:28 pm
by mekha
Nope :S... they are 2 different columns! :S..the query is 100%...the problem with the max(); what to do before or after :\

Re: Problem with max() in php :S

Posted: Sat Jul 07, 2012 7:43 pm
by Christopher
Probably no rows are selected. Do:

Code: Select all

	$me = array();
	while($row = mysql_fetch_array($query)){
		$me[] = $row['hits'];
	}
	$max = max($me);

Re: Problem with max() in php :S

Posted: Sat Jul 07, 2012 9:39 pm
by requinix
Why are you grouping the results? Why not just do a COUNT() over everything? One query and it will always return a row.

Re: Problem with max() in php :S

Posted: Sun Jul 08, 2012 2:32 am
by mekha
"Christopher" hi,
i tryed to do i got:
Warning: max() [function.max]: Array must contain at least one element in /home/rrrrrrrrrrrrrr/domains/topline.me/public_html/poll/poll.php on line 18

Re: Problem with max() in php :S

Posted: Sun Jul 08, 2012 2:38 am
by mekha
"requinix" hi,
thank you man...i tried to do this:

Code: Select all

$query = mysql_query("SELECT COUNT(`id`) as hits FROM `responses`");
        while($row = mysql_fetch_array($query)){
                $me[] = $row['hits'];
        }
        $max = max($me);
and i succeed :) ... thank you!,but are the query is right ?

Re: Problem with max() in php :S

Posted: Sun Jul 08, 2012 4:06 am
by requinix
The SQL is right but your code is much more complicated than it needs to be.

Code: Select all

$row = mysql_query("SELECT COUNT(1) FROM `responses`");
$max = current(mysql_fetch_array($row));
I also can't help but notice you don't even use this $max anywhere...

Re: Problem with max() in php :S

Posted: Sun Jul 08, 2012 8:31 am
by mikosiko
looking your selects is not even clear which your objectives are, therefore is not possible to tell you which select you must use or if you have to use group by or not... clearly you have the tables POLLS, QUESTIONS and RESPONSES and obviously they are related.... so, what are you trying to get with all those queries?