469,622 Members | 2,119 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,622 developers. It's quick & easy.

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

38
I am connecting to my database using Object oriented PHP. My query is returning results but at the end of my table,at the bottom of the page I keep having this error when I run my program: Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\mysql_class_2.php on line 45.

Can anyone help me?

The following is my PHP class:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. class mysql {
  3. ////////////////////////////////////////////////////////////////
  4. function Connect($host, $name, $pass, $db){
  5. $connection = mysql_connect("$host","$name","$pass");
  6. mysql_select_db("$db", $connection);
  7. }//ends the connection function
  8. ///////////////////////////////////////////////////////////////
  9. function Close(){
  10. mysql_close($this->connection);
  11. }//ends the close function
  12. //////////////////////////////////////////////////////////////
  13. function FetchRow($query){
  14. $rows = mysql_fetch_row($query);
  15. return $rows;
  16. }
  17. ////////////////////////////////////////////////////////////
  18. function FetchArray($query){
  19. $array = mysql_fetch_array($query);
  20. return $array;
  21. }
  22. ///////////////////////////////////////////////////////////
  23. function FetchNum($query){
  24. while ($num = mysql_num_rows($query));
  25. return $num;
  26. }
  27. ////////////////////////////////////////////////////////////////
  28. function Query($sql){
  29. $query = mysql_query($sql) or die(mysql_error());
  30. return $query;
  31. }//ends the query function
  32. ///////////////////////////////////////////////////////////////////////////////
  33. function FetchAssoc($query2){
  34. $array = mysql_fetch_assoc($query2);
  35. return $array;
  36. }
  37. ///////////////////////////////////////////////////////////////////////////////
  38. function TanimotoSimilarity($query,$input_arrays) {
  39. $i=0;
  40. $row= array();
  41. while( $row=mysql_fetch_assoc($query) )
  42. {
  43. $first[$i]=(count(array_intersect($row,$input_arrays)))/(count($row)+count($input_arrays)-count(array_intersect($row,$input_arrays)));
  44. $i++;
  45. }
  46. return($first);
  47.  
  48. }
  49. ///////////////////////////////////////////////////////////////////////////////////
  50. }//ends the class
  51. ?>
  52.  
On the code above Mysql_fetch_asson function is on line 33.

Thanks
Jan 8 '09 #1
2 16579
Atli
5,058 Expert 4TB
Hi.

You get that warning when you try to fetch rows from a variable that is not a MySQL query resource.
(A MySQL query resource is the return value of a successful mysql_query call)

To avoid it, you need to make sure that the parameter you are passing it is in fact a valid resource. This usually works fine:
Expand|Select|Wrap|Line Numbers
  1. if($resource and !is_bool($resource)) {
  2.   $row = mysql_fetch_assoc($resource);
  3. }
When a mysql_query call fails, it returns FALSE, or in the case of successful queries that do not return a result set, TRUE.

P.S.
What's with the while loop on line 24?
Jan 8 '09 #2
poreko
38
The while loop in line 24 is my mistake. It was just me trying something out.
Jan 8 '09 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

By using this site, you agree to our Privacy Policy and Terms of Use.