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.
在运行查询以构建图表时。查询从mysql数据库获取数据并使用它来构建图表。
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?
通常,我收到此错误并转到代码并找到我搞砸了,修复它并继续前进的地方。关于这个问题的棘手部分是查询实际上正在运行,并且正在构建和准确显示图表。为什么我的服务器(xampp上的localhost)告诉我,当它可以正常使用资源时查询结果是坏的?
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 个解决方案
#1
2
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.
这将告诉你何时失败。你是对的,你得到的消息是因为mysql_query返回“false”并且不是有效的结果资源。
#2
1
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.
因为你在一个循环中查询,所以没有处理其中一个术语(可能是因为search_terms缺少特定转弯的行。这很奇怪,因为你正在查询同一个表。
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.
无论哪种方式,这似乎是提取数据的错误方法,您可以使用一个查询,直接在SQL服务器中进行充分排序(ORDER BY),GROUP BY和SUM()来获取您的术语总和。
You should read up on your SQL instead :)
你应该读一下你的SQL :)
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.
然后只需将其复制到您的哈希表,它应该已经排序,并限制为10个条目。
#1
2
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.
这将告诉你何时失败。你是对的,你得到的消息是因为mysql_query返回“false”并且不是有效的结果资源。
#2
1
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.
因为你在一个循环中查询,所以没有处理其中一个术语(可能是因为search_terms缺少特定转弯的行。这很奇怪,因为你正在查询同一个表。
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.
无论哪种方式,这似乎是提取数据的错误方法,您可以使用一个查询,直接在SQL服务器中进行充分排序(ORDER BY),GROUP BY和SUM()来获取您的术语总和。
You should read up on your SQL instead :)
你应该读一下你的SQL :)
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.
然后只需将其复制到您的哈希表,它应该已经排序,并限制为10个条目。