SunQuest
 
           MySQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesMySQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old September 10th, 2004, 09:29 AM
tonyoevans tonyoevans is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 2 tonyoevans User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Help - mysql_fetch_array(): 8 is not a valid MySQL result resource

Can anyone help, I have a PHP script that returns a list of products categorys and in doing so it also needs to display the number products within those categories.

The trouble I have is that it displays the first category and products within that category and thats it. The error it comes back with is "mysql_fetch_array(): 8 is not a valid MySQL result resource" however when I remove the following two lines it displays all the categories (but obviously not the number of products within them)

$countResult = mysql_query("SELECT count(*) AS num FROM products WHERE cat_id=$cat_id",$conn);
$num = mysql_result($countResult,0,"num");


The script is:-

function cat_list($conn)
{
global $db_name;
global $db_prod_table;
global $db_cat_table;

$sql = "SELECT * FROM $db_cat_table ORDER BY cat_name";
$result = mysql_query($sql,$conn) or die (mysql_error());

$cat_list ="<table width='800' border='0' cellspacing='2' cellpadding='4'>
<tr>
<td width='50' bgcolor='#000099'><div align='center'><font face='Arial' size='1' color='#FFFFFF'>ID</font></div></td>
<td width='450' bgcolor='#000099'><div align='center'><font face='Arial' size='1' color='#FFFFFF'><b>Category Name</b></font></div></td>
<td width='200' bgcolor='#000099'><div align='center'><font face='Arial' size='1' color='#FFFFFF'><b>Number of products</b></font></div></td>
</td width='50'></td>
</td width='50'></td>
</tr>";

while ($row = mysql_fetch_array($result))
{
extract($row);

// remove the following 2 lines and it displays all the categories
$countResult = mysql_query("SELECT count(*) AS num FROM products WHERE cat_id=$cat_id",$conn);
$num = mysql_result($countResult,0,"num");

$cat_list .=
"<tr>
<td width='50' height='20'><div align='center'><font face='Arial' size='1' color='#FFFFFF'>$cat_id</font></div></td>
<td width='450'><font face='Arial' size='1' color='#FFFFFF'><b>$cat_name</b></font></td>
<td width='200' align='center'><font face='Arial' size='1' color='#FFFFFF'><b>$num</b></font></td>
<td width='50' align='center'>
<b><font face='Arial' size='1' color='#FFFFFF'><a href=confirm_delete.php?cat_id=$cat_id&catProducts=$num>Delete</a></font></b>
</td>
<td width='50' bgcolor='#669900' align='center'>
<b><font face='Arial' size='1' color='#FFFFFF'><a href=modify_cat.php?cat_id=$cat_id>Edit</a></font></b>
</td>
</tr>";
}
$cat_list .="</table>";
return $cat_list;
}


Can anyone help?

Reply With Quote
  #2  
Old September 16th, 2004, 01:13 PM
oyejorge oyejorge is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 25 oyejorge User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 m 13 sec
Reputation Power: 0
I would recommend getting rid of the SQL query within the "while" loop completely.
You can do this by using a query with a "GROUP BY" clause and "COUNT()" function.
It looks like you'll have to use a table join as well.

Below is a breakdown of a query that could work.

Code:
 
SELECT *, COUNT(*) 
 
FROM {$db_cat_table}, products 
 
WHERE {$db_cat_table}.cat_id = products.cat_id
 
GROUP BY {$db_cat_table}.cat_id
 
ORDER BY {$db_cat_table}.cat_name

MySQL Manual: http://dev.mysql.com/doc/mysql/en/index.html (chapters 10-13!)

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesMySQL Development > Help - mysql_fetch_array(): 8 is not a valid MySQL result resource


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway