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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chemlight
    New Member
    • Jan 2009
    • 33

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

    Code:
    I'm having a problem. I'm sure I'm going to kick myself over the answer...
    
    I have a table that stores vendors and their languages. This table starts out blank. I am querying the table to see if a vendor has been added to the table yet. The problem is, if they haven't been added, I can't seem to get the script to realize that. here is what I am trying to do.
    
    $testvend = SELECT language FROM vendor_details WHERE id = $vendorid
    
    if(!$testvend){
    //script to add vendor to list, with supplied language
    }else{
    while($row = mysql_fetch_array($testvend)){
    //script to get vendor language and display vendor langauge
    }
    }
    The result I get is:

    Warning: mysql_fetch_arr ay(): supplied argument is not a valid MySQL result source in (script name) on line (line with mysql_fetch_arr ay)

    *NOTE: I did try searching bytes.com and the web. The search on bytes.com returns a blank page. The web showed my method above, as well as the option to add "or die" after the query. adding or die didn't do anything either. I've also tried manually adding information to the table, so that the script is not working off of a blank table, but I still get the same result if the vendor does not exist. Also, I apologize that the title isn't very clear. I realized after I posted it I should have put a problem description in the title...
    Last edited by Markus; Mar 5 '09, 08:21 PM. Reason: Added [code] tags.
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    You need to suply the mysql_* functions with a result resource. Simply passing it the query string will not do.

    Code:
    // SQL.
    $sql = "SELECT * FROM `tbl1`"
    
    // Create result resource
    $query = mysql_query( $sql );
    
    // foreach traverse here.
    Also, please wrap code with [code] [/code] tags and give your threads a meaningful title.

    Moderator.

    Comment

    • TheServant
      Recognized Expert Top Contributor
      • Feb 2008
      • 1168

      #3
      I know this has been already said, but took me a second to work out the problem. Your code should be:
      Code:
      $testvend = mysql_query(" SELECT language FROM vendor_details WHERE id = '$vendorid' " );
      Or:
      Code:
      $testquery = SELECT language FROM vendor_details WHERE id = '$vendorid';
      $testvend = mysql_query( $testquery );
      Also, I think that php variables need to be surrounded by single quotes ( ' ), as well as the whole statement being a string - surrounded by double quotes ( " ). Also I see no semicolons " ; "? Was that a mistype or are you decalring variables without that? If so, I don't know why it's not throwing an error up for that but it should.

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        Originally posted by TheServant
        I know this has been already said, but took me a second to work out the problem. Your code should be:
        Code:
        $testvend = mysql_query(" SELECT language FROM vendor_details WHERE id = '$vendorid' " );
        Or:
        Code:
        $testquery = SELECT language FROM vendor_details WHERE id = '$vendorid';
        $testvend = mysql_query( $testquery );
        Deja vu :)

        Originally posted by TheServant
        Also, I think that php variables need to be surrounded by single quotes ( ' ), as well as the whole statement being a string - surrounded by double quotes ( " ).
        If you surround a variable that your database expects to be an int type, you will get an error. So only variables that are strings need quotes.

        Originally posted by TheServant
        Also I see no semicolons " ; "? Was that a mistype or are you decalring variables without that? If so, I don't know why it's not throwing an error up for that but it should.
        I think he just threw it together quickly to show us.

        Comment

        • TheServant
          Recognized Expert Top Contributor
          • Feb 2008
          • 1168

          #5
          Originally posted by Markus
          If you surround a variable that your database expects to be an int type, you will get an error. So only variables that are strings need quotes.
          Interesting. I will have to test because I '$quote' all my variables and have not had any trouble. Thanks for the info.
          I think he just threw it together quickly to show us.
          That's what I thought, but just better make sure ;)

          Comment

          • chemlight
            New Member
            • Jan 2009
            • 33

            #6
            Yeah, I did just throw it together quickly, thats why they weren't in there.

            My actual mistake was that I was leaving out the mysql_query part, so I was assigning the words in the query to the variable, instead of the actual results.

            I'm having another issue though.

            Here is my code

            Code:
            $testvend = mysql_query("SELECT language FROM vendor_details WHERE id = '" . $vendor . " ' ");
            if(!$testvend){
            //script if vendor does not exist
            }else{
            while($row = mysql_fetch_array($testvend)){
            //script to do if vendor exists
            }
            }
            The problem is, even if the vendor doesn't exist, it executes the code as if it did. I've tried using
            Code:
            if(count(mysql_fetch_array($testvend)) > 0){
            //script to do if vendor exists
            }
            But it seems to return a value greater than 0 anyway. I'm working on a test page that will do a better job of showing my issues, and I'll post the code here, and the results, when I get it done.

            Thanks again for the responses!
            Last edited by chemlight; Mar 5 '09, 11:01 PM. Reason: getting code tags right

            Comment

            • chemlight
              New Member
              • Jan 2009
              • 33

              #7
              Results from testing

              Here is the code I am testing with:
              (copied and pasted)
              Code:
              <?php
              
              	require("scripts/db_connect.php");
              
              	$vendor = 4486;
              	
              	$result = mysql_query("SELECT * FROM vendor_details WHERE id = '" . $vendor . "'");
              	
              	if(!$result){
              		echo "no vendor " . $vendor . "<br />";
              	}else{
              		echo "vendor " . $vendor . " exists<br />";
              	}
              
              	print_r(mysql_fetch_array($result));
              	echo "<br />";
              	
              	$vendor = 6510;
              	
              	$result = mysql_query("SELECT * FROM vendor_details WHERE id = '" . $vendor . "'");
              	
              	if(!$result){
              		echo "no vendor " . $vendor . "<br />";
              	}else{
              		echo "vendor " . $vendor . " exists<br />";
              	}
              	
              	print_r(mysql_fetch_array($result));
              ?>
              Here are the results:

              vendor 4486 exists
              Array ( [0] => 4486 [id] => 4486 [1] => [phone] => [2] => [email] => [3] => [contact] => [4] => German [language] => German [5] => 0 [rating] => 0 )
              vendor 6510 exists

              And this is my table (copied from phpmyadmin, formatted with | for easier reading - there is only one item it right now)

              id | phone | email | contact | language | rating
              4486 | | | | German | 0

              So, I need to find a way to tell the script that there is no vendor, if there isn't one...

              Comment

              • chemlight
                New Member
                • Jan 2009
                • 33

                #8
                I also decided to test it by providing a variable to the query string istead. I got the same results

                Code:
                <?php
                	require("scripts/db_connect.php");
                
                	$vendor = 4486;	
                	$result = mysql_query("SELECT * FROM vendor_details WHERE id = " . $vendor . "");
                	if(!$result){
                		echo "no vendor " . $vendor . "<br />";
                	}else{
                		echo "vendor " . $vendor . " exists<br />";
                	}
                	while($row = mysql_fetch_array($result)){
                		echo "vendor language " . $row['language'];
                	}
                	print_r(mysql_fetch_array($result));
                	echo "<br />";
                	
                	$query = "SELECT * FROM vendor_details WHERE id = " . $vendor;
                	$result = mysql_query($query);
                	if(!$result){
                		echo "no vendor " . $vendor . "<br />";
                	}else{
                		echo "vendor " . $vendor . " exists<br />";
                	}
                	while($row = mysql_fetch_array($result)){
                		echo "vendor language " . $row['language'];
                	}
                	print_r(mysql_fetch_array($result));
                	echo "<br />";
                	
                	$vendor = 6510;
                	$result = mysql_query("SELECT * FROM vendor_details WHERE id = " . $vendor . "");
                	if(!$result){
                		echo "no vendor " . $vendor . "<br />";
                	}else{
                		echo "vendor " . $vendor . " exists<br />";
                	}
                	while($row = mysql_fetch_array($result)){
                		echo "vendor language" . $row['language'];
                	}
                	print_r(mysql_fetch_array($result));
                	echo "<br />";
                	
                	$query = "SELECT * FROM vendor_details WHERE id = " . $vendor;
                	$result = mysql_query($query);
                	if(!$result){
                		echo "no vendor " . $vendor . "<br />";
                	}else{
                		echo "vendor " . $vendor . " exists<br />";
                	}
                	while($row = mysql_fetch_array($result)){
                		echo "vendor language " . $row['language'];
                	}
                	print_r(mysql_fetch_array($result));
                	echo "<br />";
                ?>
                results:
                vendor 4486 exists
                vendor language German
                vendor 4486 exists
                vendor language German
                vendor 6510 exists

                vendor 6510 exists

                I took out the print_r because I figured it would be easier to read. I still get the same issue - it keeps telling me the vendor exists when it doesn't.

                Comment

                • Markus
                  Recognized Expert Expert
                  • Jun 2007
                  • 6092

                  #9
                  mysql_num_rows( $result_resourc e) will tell you the amount of rows affected by the query. So, just run an if conditional, checking that mysql_num_rows( $result_resourc e) turns more than 0 rows.

                  Comment

                  • chemlight
                    New Member
                    • Jan 2009
                    • 33

                    #10
                    That worked. I was also just about to post that testing if the resource is null also works.

                    One of the things I had tried was:
                    Code:
                    if(count(mysql_fetch_array($result)) > 0)
                    But that returns 1 in both cases. The mysql_num_rows returns the proper count.

                    Thanks again for your help and patience!

                    Comment

                    • TheServant
                      Recognized Expert Top Contributor
                      • Feb 2008
                      • 1168

                      #11
                      lol, didn't realize it worked like this but it makes sense. mysql_fetch_arr ay() will fetch an array as the name suggests. If the array is empty and you count it, it counts 1 empty array, returning 1. So mysql_num_rows( ) is the way to go.

                      Comment

                      • Markus
                        Recognized Expert Expert
                        • Jun 2007
                        • 6092

                        #12
                        Glad we could be of help. It's always smarter to stick to methods that are there for a specific purpose (using mysql_* functions for mysql related stuff, etc.)

                        - Mark.

                        Comment

                        Working...