How to fix: "Warning: mysql_fetch_assoc(): supplied argument is not a valid . . "?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • wilsonh
    New Member
    • Dec 2010
    • 6

    How to fix: "Warning: mysql_fetch_assoc(): supplied argument is not a valid . . "?

    Hi

    I am aware that this is a common problem as I have scoured the internet endlessly looking for a solution.

    The script was installed and set up for me because I am a complete beginner so unfortunately I have made little sense of some of the solutions that have been posted.

    I have looked into it and now have some understanding of what the code should be doing or what it is trying to do.


    The error is

    "Warning: mysql_fetch_ass oc(): supplied argument is not a valid MySQL result resource in /path/to/file/index.php on line 213"

    Line 213 is....

    Code:
    while($row = mysql_fetch_assoc($rs)) {
    This is strange because this script originally worked absolutely fine until I tried uploading a new design for the index page (with the original php code re-inserted).
    I have even uploaded the original files (but left the db intact) and the error still occurs.

    I spoke to a friend of mine and he mentioned something about the variable being returned as false and I need to perform some type of operation on the database maybe.

    Do you have any suggestions on what I need to do?

    This is the code that connects to the db...

    Code:
    $conn = mysql_connect($hostname,$username,$password) or die("Having problem connecting to database");
    	mysql_select_db($dbname,$conn);
    I can also provide the full code by request.

    Thanks in advance

    Will
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    the problem likely arises in a mysql_query() call. die with an error message there.
    Code:
    $res = mysql_query($sql) or die("Error in $sql: " . mysql_error());

    Comment

    • wilsonh
      New Member
      • Dec 2010
      • 6

      #3
      Thanks for your reply Dormilich but I am still unclear as to what I need to do

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        try the following. go to line #213 in index.php, look for mysql_query() that is called before that, insert/append the code from post#2, post result here.

        Comment

        • wilsonh
          New Member
          • Dec 2010
          • 6

          #5
          There is no mysql_query that I can see.
          I added your code just before line 213. This has removed the original error message but it now displays....

          "Error in select name,expiration ,ext from domains where 1=1: Lost connection to MySQL server during query"

          Is there anyway I can show you the whole code of index.php?
          There are about 250 lines of code which are too many to post here.

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            you can put it in a zip file and attach it. post the zip file’s checksum too (sha1 preferred)

            Comment

            • wilsonh
              New Member
              • Dec 2010
              • 6

              #7
              Thanks Dormilich, I have attached the files as requested.
              There are 2 other files included in the zip (files linked to the index which may be of some help).
              Attached Files

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                the problem here is improper class design.

                the method PS_Pagination->paginate() (ps_pagination. php:60) does not return a consistent result. if the query succeeds, it returns a resource, if the query fails (what is happening here) it returns false (just like mysql_query()). therefore as immediate fix you have to check the $rs variable (index.php:191) before using it (index.php:213) .

                if you’re running PHP 5, I recommend a different pagination class (this code is for PHP 4). in PHP 5 there are enough mechanisms available to avoid those problems, but coding all those in the current code would be a waste of effort. IMO, this class’ code needs a complete rewrite.

                Comment

                • wilsonh
                  New Member
                  • Dec 2010
                  • 6

                  #9
                  Thanks for having a look, I really appreciate it!

                  Can you tell me how I would check the $rs variable?

                  Also, for a re-write is this a big job that I would need to pay somebody to do or could I find the code online?

                  Sorry to keep asking questions, I am a complete beginner with php and mysql

                  Comment

                  • Dormilich
                    Recognized Expert Expert
                    • Aug 2008
                    • 8694

                    #10
                    check the variable:
                    Code:
                    // PHP 5
                    if (!$rs) 
                    {
                        throw new RuntimeException("The Query failed.");
                    }
                    this requires your code wrapped in a try … catch block. (cf. Exceptions)

                    rewrite, first you should search the net for other pagination classes. chances are that you’ll find something suitable. esp. pay attention to error handling.

                    the only thing I could offer you is a solid database handling system for PHP 5 …

                    Comment

                    • wilsonh
                      New Member
                      • Dec 2010
                      • 6

                      #11
                      Ok thanks for that!
                      I'll have a scour around and see if I can find anything!

                      Comment

                      Working...