Problem with max() in php :S

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mekha
Forum Contributor
Posts: 112
Joined: Sat Mar 31, 2012 6:50 am

Problem with max() in php :S

Post 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);
?>



mekha
Forum Contributor
Posts: 112
Joined: Sat Mar 31, 2012 6:50 am

Re: Problem with max() in php :S

Post 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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Problem with max() in php :S

Post by Christopher »

Shouldn't it be counting qid?

Code: Select all

SELECT COUNT(`qid`) as hits FROM `responses` GROUP BY `qid`");
(#10850)
mekha
Forum Contributor
Posts: 112
Joined: Sat Mar 31, 2012 6:50 am

Re: Problem with max() in php :S

Post 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 :\
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Problem with max() in php :S

Post 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);
(#10850)
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Problem with max() in php :S

Post 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.
mekha
Forum Contributor
Posts: 112
Joined: Sat Mar 31, 2012 6:50 am

Re: Problem with max() in php :S

Post 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
mekha
Forum Contributor
Posts: 112
Joined: Sat Mar 31, 2012 6:50 am

Re: Problem with max() in php :S

Post 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 ?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Problem with max() in php :S

Post 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...
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: Problem with max() in php :S

Post 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?
Post Reply