Jump to content

Recommended Posts

I recently had a hard drive failure on our web server and after restoring the backup have an odd problem with a MySQL/php form that worked perfectly in the past. (I'm not much of a coder and have tried to incorporate an 'else' statement to get more error information out of MySQL but I keep getting a T_ELSE error so I'm probably not putting it in the right place. It's not in there at the moment.)

The error I'm getting is "Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/stj/public_html/school/ar/print-shell.php on line 42." Line 42 is the "while ($row = mysql_fetch_array($result)) [" line.

I'm looking for a recommendation on where to include the else to get more error information, or anything obvious that could be causing the problem. As I mentioned, this exact identical code was working -- AFAIK -- before the HDD failure so I'm really stumped. Here's the URL: [a href=\"http://stjuliana.org/school/ar/index.php\" target=\"_blank\"]http://stjuliana.org/school/ar/index.php[/a]


Here's my code (it's probably VERY inelegant but at least it used to work :P):
[code]<?php
    $db = mysql_connect("localhost", "XXXX", "XXXX")
        or die("Unable to connect to MySQL Server");
        mysql_select_db("stj_ar", $db) or
        die("Unable to select database");
?>
<tr><td colspan="5" class="new" align="center" class="t"><b>
<?php
    if ($gobutton == "Grade") {
       echo("Books in Grade: $selgrade</b><br><br></td></tr>");
       $result = mysql_query("SELECT * FROM list where grade = '" . $selgrade . "' ORDER BY point asc");
       }
    elseif ($gobutton == "Point") {
       echo("Books of $selpoint or more AR points</b><br><br></td></tr>");
       $result = mysql_query("SELECT * FROM list where point >= '" . $selpoint . "' ORDER BY point asc");
       }
    elseif ($gobutton == "Level") {
       echo("Books of: $sellevel or higher reading level</b><br><br></td></tr>");
       $result = mysql_query("SELECT * FROM list where level >= '" . $sellevel . "' ORDER BY level asc, point asc");
       }
    elseif ($gobutton == "Author") {
       echo("Entire AR list, sorted by author's name:</b><br><br></td></tr>");
       $result = mysql_query("SELECT * FROM list ORDER BY author_l asc, author_f asc, name asc");
        }
    elseif ($gobutton == "Title") {
       echo("Entire AR list, sorted by book title:</b><br><br></td></tr>");
       $result = mysql_query("SELECT * FROM list ORDER BY name asc");
       }
        while ($row = mysql_fetch_array($result)) {
                if ($alternate == "1") {
                    $color = "#ffffff";
                    $alternate = "2";
                }
                else {
                    $color = "#d2dbe3";
                    $alternate = "1";
                }
            $sp = " ";
            $td  = "<td class=\"l\">";
            $tdb  = "<td class=\"l\"><b>";
            $tdc  = "<td class=\"l\" align=\"center\" >";
            $tdr = "<td class=\"l\" align=\"right\" >";
            $tr = "<tr bgcolor=$color>";
            echo("$tr $tdr $row[grade] $sp $sp </td> $td  $row[name]</td> $td $row[author_l], $row[author_f]</td> $tdr $row[level] $sp $sp </td> $tdr $row[point] $sp $sp </td></tr>");
            }
            
?>
[/code]

Many thanks for any suggestions on how to fix this.

~Jacy
Link to comment
https://forums.phpfreaks.com/topic/3258-warning-not-a-valid-resource/
Share on other sites

Well, sounds like there's a problem with the query. Before the while() loop, you should see if there were any errors (use mysql_error() to check), and also verify that $result is a valid resource -- which, apparently, it is not. Don't be misled by the HDD failure -- it's probably a red herring, unless it managed to corrupt your tables.
Thanks for the speedy reply!

After a bunch more investigating I think there's a problem with my html. I've pasted the SQL queries directly into phpMyAdmin and everything runs just fine there. And I can pull the entire DB in on the first request -- [a href=\"http://stjuliana.org/school/ar/print-old.php\" target=\"_blank\"]http://stjuliana.org/school/ar/print-old.php[/a] -- with the default sorting and grouping, so I know the DB is not corrupt.

When I start to play with the sorting and grouping within my html, either in URLs or with form submits, is where it falls down. Any of the grouped/sorted queries below sort/group perfectly within phpMyAdmin, but within my html all I ever get is the default view ($result = SELECT * FROM list ORDER BY name asc), no matter how I try to sort and group

I know for a fact it was working at one point in the past; last time I can remember checking it was November of 2005, so I'm really stumped as I haven't edited the bulk of this file in ages. I use Dreamweaver, though, and had some trouble with some editable library/template sections going bad on this entire site so I wonder if Dreamweaver didn't rewrite something out of here that I just can't see.

[code]
$result = mysql_query("SELECT * FROM list ORDER BY grade asc, name asc");
$sort = "Grouped by grade and then sorted by book name";

if ($view == "name_s") {
   $result = mysql_query("SELECT * FROM list ORDER BY name asc");
   $sort = "Sorted by Book Name";
}
elseif ($view == "author_s") {
   $result = mysql_query("SELECT * FROM list ORDER BY grade asc, author_l asc");
   $sort = "Grouped by Grade, Sorted by Author";
}
elseif ($view == "author_ns") {
   $result = mysql_query("SELECT * FROM list ORDER BY author_l asc");
   $sort = "Sorted by Author";
}
elseif ($view == "level_s") {
   $result = mysql_query("SELECT * FROM list ORDER BY grade asc, level asc");
   $sort = "Grouped by Grade, Sorted by Book Level";
}
elseif ($view == "level_ns") {
   $result = mysql_query("SELECT * FROM list ORDER BY level asc");
   $sort = "Sorted by Book Level";
}
elseif ($view == "point_s") {
   $result = mysql_query("SELECT * FROM list ORDER BY grade asc, point asc");
   $sort = "Grouped by Grade, Sorted by Point Value";
}
elseif ($view == "point_ns") {
   $result = mysql_query("SELECT * FROM list ORDER BY point asc");
   $sort = "Sorted by Point Value";
}
[/code]

~Jacy
No, it didn't solve the problem -- I can still reproduce the original error. I have a couple different versions of this basic page out there; one that's form inputs for the sorting and the other that's just links, but all incorporate some sort of view variable.

Wonder if the fact that a newer version of php is now running on my server could possibly be at fault. As I said, last known good output was in November of 2005, and in December a new php build was installed, which broke a couple other things, which is why i remember. I never throught to check this page in December.

~Jacy
Sorry for the confusion.

There are two versions of HTML and PHP that I've used for this particular page; one is printer-friendly and the other not.

When I use the first code example mentioned above I receive the invalid resource error. However, I can copy and paste the queries within that example directly into phpMyAdmin and NOT get an invalid resource error so I know it's not really an invalid resource. That particular example uses a form and buttons for user input.

The second code example is one I've also used in the past and the first run of this (default view is to see every record in the table) works perfectly. When I attempt to use any of the sorting options (the $view) it breaks. This one uses text links with the desired $view passed in the URL to handle the sorting query.

Maybe I should just ditch the code entirely and start from scratch.

~Jacy

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.