Mysql is throwing a query error, but finishing the query just fine?

I get

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

      

When you run a query to build a chart. The query gets data from mysql db and uses it to build a chart.

I usually get this error and go to the code and find where I squinted, fix it and go. The tricky part of this problem is that the query is actually executed and the graph is plotted and displayed accurately. Why is my server (localhost on xampp) telling me the request result is bad when it can use the resource just fine?

Here's the complete 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); 

      

0


source to share


2 answers


Do it:

$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();

      

This will tell you when it happens. You are correct, but you are getting this message because mysql_query returned "false" and is not a valid result resource.

+2


source


Since your query is in a loop, one of the terms is not being processed (probably because there are no rows in search_terms for that particular move. This is odd since you are querying the same table.

However, since this is a warning and not a fatal error, it will still continue.

Anyway, this seems like the wrong way to pull your data, you can probably do a single query, an adequate sort (ORDER BY) directly on the SQL server, GROUP BY and SUM () to get the sum of your conditions.



You should read 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 table and it should already be sorted and also limited to 10 records.

+1


source







All Articles