Shoutbox

Whats wrong with this php code? - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: Whats wrong with this php code? (/showthread.php?tid=80621)

Whats wrong with this php code? by Quantum on 01-02-2008 at 06:34 PM

code:
//Tags//

$query_tags = mysql_query("SELECT id, name FROM pp_tags GROUP BY name ORDER BY COUNT(id) DESC LIMIT 0,50");

while ($row_tag = mysql_fetch_array($query_tags)) {
$tags[] = $row_tag;
}
$smarty->assign('tag', $tags);
//tags end

Thanks for your help :)
RE: Whats wrong with this php code? by saralk on 01-02-2008 at 06:46 PM

I can't really help without the error messages, and it's a while since I have used php, but I don't think you can copy an array like that. You would need some sort of counter.

for example

code:
$i = 0;
while ($row_tag = mysql_fetch_array($query_tags)) {
$tags[$i] = $row_tag;
}


I can't remember if $row_tag is an array or not. But something along those lines.

RE: Whats wrong with this php code? by Quantum on 01-02-2008 at 06:50 PM

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in [FILE LOCATION]


RE: Whats wrong with this php code? by MeEtc on 01-02-2008 at 08:09 PM

Try running the query in PMA. It will fail there. You are sorting by COUNT(id) when you are not declaring it in the SELECT clause. and COUNT(id) will be a constant number anyway.


RE: Whats wrong with this php code? by win_crook on 01-02-2008 at 08:21 PM

code:
$query_tags = mysql_query("SELECT COUNT(id) AS count_id, name FROM pp_tags GROUP BY name ORDER BY count_id DESC LIMIT 0,50");


That should work for the query ;)

Also that array copying code doesn't look right either like saralk was saying.

So try this:

code:
$i = 0;
while ($row_tag = mysql_fetch_array($query_tags)) {
$tags[$i] = $row_tag;
$i++;
}


You need to overwrite your while loop with that second piece of code.
Good Luck (Y)
RE: Whats wrong with this php code? by Quantum on 01-02-2008 at 09:11 PM

while ($row_tag = mysql_fetch_array($query_tags)) {

It says that, that line is the wrong code.


RE: Whats wrong with this php code? by win_crook on 01-02-2008 at 09:52 PM

Yeah.. it's saying it can't get the results from the query because the query failed. So you need to find out what is wrong with the query. Did you try replacing it with that code I gave you?

Or you could try and see if mysql_error() returns anything.


RE: Whats wrong with this php code? by ShawnZ on 01-02-2008 at 10:40 PM

quote:
Originally posted by win_crook
Also that array copying code doesn't look right either like saralk was saying.

huh? what doesn't look right about it?
RE: Whats wrong with this php code? by Quantum on 01-02-2008 at 11:36 PM

$row = mysql_fetch_array($confresult);

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource

Ok whats wrong  with this.


RE: Whats wrong with this php code? by Ezra on 01-02-2008 at 11:41 PM

Why are you ordering your results by the number of rows?

The order by should just be id of you want to order it by that column. Don't try to order it by a number that will likely not exist as a column

$query_tags = mysql_query("SELECT id, name FROM pp_tags GROUP BY name ORDER BY id DESC LIMIT 0,50");


RE: Whats wrong with this php code? by -dt- on 01-03-2008 at 01:37 AM

quote:
Originally posted by saralk
I can't really help without the error messages, and it's a while since I have used php, but I don't think you can copy an array like that. You would need some sort of counter.
yes you can $Array[] = 'aa'; is just like using Array_push($Array, 'aa');
RE: RE: Whats wrong with this php code? by andrewdodd13 on 01-03-2008 at 12:47 PM

quote:
Originally posted by john-t
$row = mysql_fetch_array($confresult);

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource

Ok whats wrong  with this.
NOTHING is wrong with this, as it's been said.

The problem is that you are passing an invalid result as $confresult fails because of your invalid SQL statement.
RE: Whats wrong with this php code? by Spunky on 01-03-2008 at 03:37 PM

Best thing to do with the query is run it in PHPMyAdmin in the SQL window. Problems will be shown and you can copy & paste when you get it working.

From my experience it'll be a quote (or back quote [`] error) for the fields...


RE: Whats wrong with this php code? by ShawnZ on 01-03-2008 at 05:26 PM

but everyone just told him what was wrong!