tags:

views:

119

answers:

2

I am getting

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result 
resource in *filename* on line 81

While running a query to build a chart. The query gets data from the mysql db and uses it to build the chart.

Usually, I get this error and go to the code and find where I've screwed up, fix it, and move on. The tricky part about this problem is that the query is actually running and the chart is being built and displayed accurately. Why is my server (localhost on xampp) telling me that the query result is bad when it can utilize the resource just fine?

Here is the full query:

$chart=array();
    $roll=array();
    //select used terms
    $rosh=mysql_query("select distinct term from search_terms");

    while($roshrow=mysql_fetch_assoc($rosh)){
     extract($roshrow);
      $roll[]=$term;
     }

     //select term_number for each term
     foreach($roll as $sterm){
      $termarray=array();
                  **//following is line 81**
      $bashq="select term_number from search_terms where term ='$sterm'";
      $bash=mysql_query($bashq);
      while($brow=mysql_fetch_assoc($bash)){
       extract($brow);
       //put results into array to sum
       $termarray[]=$term_number;
      }
      $termsum=array_sum($termarray);

     //put term=>number array for chart script
      $chart[$sterm]=$termsum;

     }
     //sort array so high numbers at beginning
     arsort($chart);

     //slice top 10 terms
     $chart=array_slice($chart,0,10);
+2  A: 

Do this:

$rosh=mysql_query("select distinct term from search_terms")
  or die("Error with query: " . mysql_error());

and this:

$bash=mysql_query($bashq)
  or die("Error with query: " . mysql_error();

That will tell you when it fails. You are correct though, you're getting that message because mysql_query has returned "false" and isn't a valid result resource.

mercutio
"d-oh" to myself. I had such a good habit of including this in my queries until I got lazy and stopped using it. the culprit?...an apostrophe in the database. Thanks
kevtrout
+1  A: 

Because your querying within a loop, one of the terms isn't processed (probably because search_terms is missing rows for that specific turn. Which is odd, since you're querying the same table.

However, since it's a Warning, and not a fatal Error, it will still continue.

Either way, it's seems like wrong way to pulling your data, you can probably do taht with one query, adequate sorting (ORDER BY) directly in the SQL server, GROUP BY and SUM() for getting the sum of your terms.

You should read up on your SQL instead :)


SELECT term, SUM(term_number) as term_sum
FROM search_terms 
GROUP BY terms 
ORDER BY term_sum DESC
LIMIT 10

then just copy it to your hashtable and it should already be sorted, aswell as limited to 10 entries.

jishi
I should read up on my sql. I give myself lots of headaches trying to sort my query results
kevtrout