Page 2 of 4 FirstFirst 1234 LastLast
Results 11 to 20 of 34

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

  1. #11
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    Your example shows the tablename as lowercase, so you seem to prove my point while claiming to disagree. (?) You are supposed to put the command reserved words in capitals and the table name in lowercase, just as you did.

    I am simply suggesting that Chris might try renaming his table from 'Pet' to 'pet'. If you read about MySQL rules here: http://dev.mysql.com/doc/refman/5.1/...nsitivity.html you'll see why they recommend using all lowercase for compatibility, portability, and to avoid having to mess with the mySQL .ini file.

  2. #12
    Join Date
    Jul 2007
    Location
    Azerbaijan, Baku
    Posts
    144
    Thanks
    11
    Thanked 27 Times in 25 Posts

    Default

    You are right they recommend. But it works ))) Actually i don't use them. But have used before...

  3. #13
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    I just noticed that Chris used capitals in his database name too. I really think that is asking for trouble. I would rename the database from PetCatalog to petcatalog and make sure all table names are lowercase. MySQL automatically converts database and table names to lowercase during backup and storage - not sure what happens when it restores. It is OK to use mixed case in field names but not database or table names.

    I once used a hyphen in a table name (eg. users-new) and nothing worked until I changed it to an underscore (users_new). Won't do that again.

  4. #14
    Join Date
    Jul 2007
    Location
    Azerbaijan, Baku
    Posts
    144
    Thanks
    11
    Thanked 27 Times in 25 Posts

    Default

    kuau, yes you are right. That is another problem of Chris. But problem what he asked, i answered in page 1 ^^.

    P.S. I'm not sure too. But logically in restore, it must go back...

  5. #15
    Join Date
    Jan 2007
    Posts
    629
    Thanks
    10
    Thanked 28 Times in 28 Posts

    Default

    Quote Originally Posted by allahverdi View Post
    umm. I think you all missed one thing.

    change this:

    Code:
    $row = mysql_fetch_array($result)
    to this:

    Code:
    $row = mysql_fetch_array($result, MYSQL_ASSOC)
    How will changing the array type help? The error is not in the type of array, it is in the resource being turned into an array-- the query is not working.

    BTW: there is nothing wrong with capital letters in Database names or table names. Think of them like variables-- you wouldn't call a variable $VARIABLE, but you could. I have used capital letters many times (not as common practice, mind, but sometimes it's just easier for readability).
    But you should never use special chars, like the hyphen, because that does cause problems.
    Last edited by Jas; 07-01-2008 at 06:51 PM.
    --Jas
    function GreatMinds(){ return "Think Like Jas"; }
    I'm gone for a while, but in the meantime: Try using my FTP script | Fight Bot Form Submissions

  6. #16
    Join Date
    Jun 2008
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    Hi all,

    And thanks ever so much for everybody's responses! I'll address each of them!

    Nile: I created a new page based on your cleaned-up code, but got the following error when I ran it:

    Parse error: syntax error, unexpected $end in C:\wamp\www\chris\petdisplay2.php on line 45

    Jas: I'm fairly sure my query is right - when I run the query by querying the MySQL db directly it returns the relevant records. I tried inserting your code:

    PHP Code:
    echo $query;
    $results MySQL_Query($query) or die(MySQL_Error()); 
    But wasn't 100% sure where I should insert it, I placed it beneath the line:

    PHP Code:
    $result mysql_query($query
    I still received the same error as before with no more helpful info. Re privileges + permissions, I've checked this and have used the root@localhost user which has full permissions but it's still giving me the error.

    Allahverdi: I tried adding MYSQL_ASSOC: as follows:

    PHP Code:
    $row mysql_fetch_array($resultMYSQL_ASSOC
    But again-no joy!

    Kuau: Re the uppercase lowercase suggestion, the book I'm working from displays the databases/tables with an initial uppercase letter, and same for tables - I've copied this in my db and as you'll see further down below I've since discovered that capital letters are not an issue.

    SO!!! Thanks for your responses. What I tried tonight after your suggestions was moving onto the next tutorial that my book offers based on near enough the same code... a slightly more advanced page which uses a for loop rather than a while loop.

    Strangely the slightly different page loaded perfectly!! Which puts to rest any debate about uppercase/lowercase, incorrect login, privileges etc.

    So, whilst this is good, my inquisitive brain isn't satisfied! Why does this code continue to fail:

    PHP Code:
    <?php
    /*    Program:    petdisplay.php
    *    Desc:        Displays all pets in selected category.
    * /
    ?>
    <html>
    <head><title>Pet Catalog</title></head>
    <body>
    <?php
        $user="root";
        $host="localhost";
        $password="";
        $database = "PetCatalog";
        $connection = mysql_connect($host,$user,$password)
            or die ("Couldn't connect to server");
        $db = mysql_select_db($database,$connection)
            or die ("Couldn't select database");
        $pettype = "horse";  //horse was typed in a form by user
        $query = "SELECT * FROM Pet WHERE petType='$pettype'";
        $result = mysql_query($query)
            or die ("Couldn't execute query.");

        /* Display results in a table */
        
    $pettype ucfirst($pettype)."s";
        echo 
    "<h1>$pettype</h1>";
        echo 
    "<table cellspacing='15'>";
        echo 
    "<tr><td colspan='3'><hr></td></tr>";
        while (
    $row mysql_fetch_array($result))
        {
            
    extract($row);
            
    $f_price number_format($price,2);
             echo 
    "<tr>\n
                    <td>
    $petName</td>\n
                    <td>
    $petDescription</td>\n
                    <td align='right'>\$
    $f_price</td>\n
                    </tr>\n"
    ;
                echo 
    "<tr><td colspan='3'><hr></td></tr>\n";
        }
        echo 
    "</table>\n";
    ?>
    </body></html>
    Whilst this code runs perfectly?!:

    PHP Code:
    <?php
    /*    Program:    petDescripFor.php
    *    Desc:        Displays a numbered list of all pets in
    *                selected category.
    */
    ?>
    <html>
    <head><title>Pet Catalog</title></head>
    <body>
    <?php
        $user
    ="root";
        
    $host="localhost";
        
    $password="";
        
    $database "PetCatalog";
        
    $connection mysql_connect($host,$user,$password)
            or die (
    "couldn't connect to server");
        
    $db mysql_select_db($database,$connection)
            or die (
    "couldn't select database");
        
    $pettype "horse";  //horse was typed in a form by user
        
    $query "SELECT * FROM Pet WHERE petType='$pettype'";
        
    $result mysql_query($query)
            or die (
    "Couldn't execute query.");
        
    $nrows mysql_num_rows($result);

        
    /* Display results in a table */
        
    echo "<h1>Horses</h1>";
        echo 
    "<table cellspacing='15'>";
        echo 
    "<tr><td colspan='4'><hr></td></tr>";
        for (
    $i=0;$i<$nrows;$i++)
        {
                
    $n $i 1;    #add 1 so that numbers don't start with 0
                
    $row mysql_fetch_array($result);
                
    extract($row);
                
    $f_price number_format($price,2);
                    echo 
    "<tr>\n
                        <td>
    $n.</td>\n
                        <td>
    $petName</td>\n
                        <td>
    $petDescription</td>\n
                        <td align='right'>\$
    $f_price</td>\n
                        </tr>\n"
    ;
                    echo 
    "<tr><td colspan='4'><hr></td></tr>\n";
        }
        echo 
    "</table>\n";
    ?>
    </body></html>
    Are there any particularly clever people who want to offer their suggestions?!
    Last edited by chriswattsuk; 07-01-2008 at 09:29 PM.

  7. #17
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Jas: I'm fairly sure my query is right - when I run the query by querying the MySQL db directly it returns the relevant records. I tried inserting your code:

    PHP Code:
    echo $query;
    $results MySQL_Query($query) or die(MySQL_Error()); 
    But wasn't 100% sure where I should insert it, I placed it beneath the line:

    PHP Code:
    $result mysql_query($query
    I still received the same error as before with no more helpful info. Re privileges + permissions, I've checked this and have used the root@localhost user which has full permissions but it's still giving me the error.
    Instead of putting it under the line, replace it with the line.
    Jeremy | jfein.net

  8. #18
    Join Date
    Jul 2007
    Location
    Azerbaijan, Baku
    Posts
    144
    Thanks
    11
    Thanked 27 Times in 25 Posts

    Default

    You mean this is your full code?
    Code:
    <?php
    /*    Program:    petdisplay.php
    *    Desc:        Displays all pets in selected category.
    * /
    ?>
    <html>
    <head><title>Pet Catalog</title></head>
    <body>
    <?php
        $user="root";
        $host="localhost";
        $password="";
        $database = "PetCatalog";
        $connection = mysql_connect($host,$user,$password)
            or die ("Couldn't connect to server");
        $db = mysql_select_db($database,$connection)
            or die ("Couldn't select database");
        $pettype = "horse";  //horse was typed in a form by user
        $query = "SELECT * FROM Pet WHERE petType='$pettype'";
        $result = mysql_query($query)
            or die ("Couldn't execute query.");
    
        /* Display results in a table */
        $pettype = ucfirst($pettype)."s";
        echo "<h1>$pettype</h1>";
        echo "<table cellspacing='15'>";
        echo "<tr><td colspan='3'><hr></td></tr>";
        while ($row = mysql_fetch_array($result))
        {
            extract($row);
            $f_price = number_format($price,2);
             echo "<tr>\n
                    <td>$petName</td>\n
                    <td>$petDescription</td>\n
                    <td align='right'>\$$f_price</td>\n
                    </tr>\n";
                echo "<tr><td colspan='3'><hr></td></tr>\n";
        }
        echo "</table>\n";
    ?>
    </body></html>
    Full copy of this???

    I copied this to dw. And guess what i see? Full comment till
    Code:
    /* Display results in a table */
    If your code full like this. (I wish it won't be). Change to it:

    Code:
    <?php
    /*    Program:    petdisplay.php
    *    Desc:        Displays all pets in selected category.
    */
    ?>
    <html>
    <head><title>Pet Catalog</title></head>
    <body>
    <?php
        $user="root";
        $host="localhost";
        $password="";
        $database = "PetCatalog";
        $connection = mysql_connect($host,$user,$password)
            or die ("Couldn't connect to server");
        $db = mysql_select_db($database,$connection)
            or die ("Couldn't select database");
        $pettype = "horse";  //horse was typed in a form by user
        $query = "SELECT * FROM Pet WHERE petType='$pettype'";
        $result = mysql_query($query)
            or die ("Couldn't execute query.");
    
        /* Display results in a table */
        $pettype = ucfirst($pettype)."s";
        echo "<h1>$pettype</h1>";
        echo "<table cellspacing='15'>";
        echo "<tr><td colspan='3'><hr></td></tr>";
        while ($row = mysql_fetch_array($result))
        {
            extract($row);
            $f_price = number_format($price,2);
             echo "<tr>\n
                    <td>$petName</td>\n
                    <td>$petDescription</td>\n
                    <td align='right'>\$$f_price</td>\n
                    </tr>\n";
                echo "<tr><td colspan='3'><hr></td></tr>\n";
        }
        echo "</table>\n";
    ?>
    Your problem was, you didn't close your comment correctly . But i think just you copied here incorrectly...

  9. The Following 2 Users Say Thank You to allahverdi For This Useful Post:

    chriswattsuk (07-02-2008),kuau (07-02-2008)

  10. #19
    Join Date
    Jun 2008
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Talking

    ALLAHVERDI!!!!!!!!!!!! I don't believe it!!! That's it!

    I'm so happy! Pathetic I know!!! Woooooaooooo!!!!

    So just for nice conclusion purposes, the error:

    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\wamp\www\chris\petdisplay.php on line 28

    Was being caused by nothing other than.... an incorrectly closed code comment:

    <?php
    /* Program: petdisplay.php
    * Desc: Displays all pets in selected category.
    * /
    ?>

    Which when changed to:

    <?php
    /* Program: petdisplay.php
    * Desc: Displays all pets in selected category.
    */
    ?>

    Worked perfectly! Thanks to everybody for their kind and helpful input! Very very strange that it showed the error as being a problem so far down the page (line 28!)

  11. #20
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    Bravo Allahverdi!!!! Talk about not seeing the forest for the trees haha. If you look back, the whole thing is bright orange. Duh!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •