Page 1 of 4 123 ... LastLast
Results 1 to 10 of 34

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

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

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

    Hi all,

    I'm a newbie so please forgive me! I've tried finding the answer to this problem for a good couple of hours but not got anywhere...

    I'm getting the following error:

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

    Code:
    <?php
    /* Program: petdisplay.php
    * Desc: Displays all pets in selected category.
    * /
    ?>
    <html>
    <head><title>Pet Catalog</title></head>
    <body>
    <?php
    	$user="catalog";
    	$host="localhost";
    	$password="p4ssword";
    	$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>
    Can anybody suggest what the problem might be? I'm working through my PHP + MySQL For Dummies book, but unfortunately the book doesn't offer any troubleshooting at this point! I've retyped the code and checked it many times but can't fathom the problem!

    The offending line (28) is:

    Code:
    while ($row = mysql_fetch_array($result))
    Many thanks in anticipation of any help!

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

    Default

    This means that your connection to the database failed. Make sure that thats correct.
    Jeremy | jfein.net

  3. The Following User Says Thank You to Nile For This Useful Post:

    chriswattsuk (06-30-2008)

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

    Default

    Thanks for your reply - I thought this piece of code throws up an error if the database connection fails?

    Code:
    $connection = mysql_connect($host,$user,$password)
    		or die ("Couldn't connect to server");
    I've checked the db connection details and they're correct - I've also tried with the root localhost user. I've also double checked the case and it all seems to tally.

    Any other ideas?

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

    Default

    Try this:
    PHP Code:
    mysql_connect($host,$user,$password)
    or die (
    "Couldn't connect to server because: ".mysql_error());
    mysql_select_db($database)
    or die (
    "Couldn't select database because: ".mysql_error()); 
    And then make sure the database has also the correct name.
    Jeremy | jfein.net

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

    Default

    I've inserted that code as described, still getting exactly the same output though on the page... no more details about 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

    Thanks for your help.

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

    Default

    Well, I'm just cleaning up your code here:
    Code:
    <?php
    /* Program: petdisplay.php
    * Desc: Displays all pets in selected category.
    * /
    ?>
    <html>
    <head><title>Pet Catalog</title></head>
    <body>
    <?php
    	$user="catalog";
    	$host="localhost";
    	$password="p4ssword";
    	$database = "PetCatalog";
    mysql_connect($host,$user,$password)
    or die ("Couldn't connect to server because: ".mysql_error());
    mysql_select_db($database)
    or die ("Couldn't select database because: ".mysql_error());
    	$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 because: ".mysql_error());
    
    	/* Display results in a table */
    	$pettype = ucfirst($pettype)."s";
    	echo <<< TABLE
            <h1>$pettype</h1>
    	<table cellspacing='15'>
    	<tr><td colspan='3'><hr></td></tr>
            TABLE;
    	while ($row = mysql_fetch_array($result))
    	{
    		extract($row);
    		$f_price = number_format($price,2);
    		 echo <<<TEXTA
                                   <tr>\n
    				<td>$petName</td>\n
    				<td>$PetDescription</td>\n
    				<td align='right'>\$$f_price</td>\n
    				</tr>\n
                                     TEXTA;
    			echo "<tr><td colspan='3'><hr></td></tr>\n";
    	}
    	echo "</table>\n";
    ?>
    </body></html>
    Jeremy | jfein.net

  8. #7
    Join Date
    Jan 2007
    Posts
    629
    Thanks
    10
    Thanked 28 Times in 28 Posts

    Default

    Nile: that post seems pointless, unless you made some corrections I am missing?

    chriswattsuk:

    Not to intrude, but that error usually has nothing to do with the connection (some times it does, though), it has to do with the query. Specifically, the message is saying that the results you are trying to turn into an array (the var $results) is not a valid MySQL result, probably because the query was wrong. Try echo-ing out the query and check for syntax errors. You could also try:

    Code:
    echo $query;
    $results = MySQL_Query($query) or die(MySQL_Error());
    And then let us know what it echo's out.

    If that doesn't work, consider the table/database privileges. Do you have access to the table/database?
    Last edited by Jas; 07-01-2008 at 03:52 AM.
    --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

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

    Default

    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)

  10. #9
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    647
    Thanks
    287
    Thanked 15 Times in 15 Posts

    Default

    I'm just a newbie but I thought you weren't allowed to use capitals in table names (?) in MySQL.

    Code:
    $query = "SELECT * FROM Pet WHERE petType='$pettype'";

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

    Default

    kuau,

    I'm not agree with you. I think it depends on table and column name.

    I have used capitals and they are working fine. For example:

    Code:
    $result = mysql_query("SELECT * FROM tablename WHERE Id = '$id'");

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
  •