SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Thread: mySQL problem

  1. #1
    [Call me Bram] iBram007's Avatar
    Join Date
    Feb 2001
    Location
    Belgium
    Posts
    339
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    my script always gives the same error:
    Warning: Supplied argument is not a valid MySQL result resource in mysql.php3 on line 84

    this is line 84::
    Code:
    if( (!$results) or (empty($results)) ) {
    mysql_free_result($results);eturn false;
    }
    dbname, username, pass, etc... is allright, but this line always gives the same error
    I'm running MySQL 3.22.32 and PHP4

  2. #2
    Grumpy Mole Man Skunk's Avatar
    Join Date
    Jan 2001
    Location
    Lawrence, Kansas
    Posts
    2,066
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    if( (!$results) or (empty($results)) ) {
    mysql_free_result($results);
    return 
    false;


  3. #3
    Grumpy Mole Man Skunk's Avatar
    Join Date
    Jan 2001
    Location
    Lawrence, Kansas
    Posts
    2,066
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ignore the <br /> tags

  4. #4
    [Call me Bram] iBram007's Avatar
    Join Date
    Feb 2001
    Location
    Belgium
    Posts
    339
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, that code doesn't work, first I placed the script also between [ php ] tags, but then SPF created </ br> tags, so I deleted that and I took something of return false...

    this doesn't work:
    Code:
    if( (!$results) or (empty($results)) ) {
    mysql_free_result($results);
    return false;
    }

  5. #5
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This suggest to me that !$results or empty($results) evaluates to true. For some reason your query has failed and no result set identifier was assigned to $results. So execution enters the if block and calls function mysql_free_result($results) which causes the error because $results is not a valid mysql results resource. So the logic is flawed. You are saying "If a result set does not exist - free the (non-existent) result set."

    BTW, what is the difference between !$results and empyt($results) - are they not more or less testing the same thing?

  6. #6
    [Call me Bram] iBram007's Avatar
    Join Date
    Feb 2001
    Location
    Belgium
    Posts
    339
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I didn't write that part by myself, it's a part of phpHoo, but this are two functions with that part of code:

    Code:
    	function select ($sql="", $column="")
    	{
    		if(empty($sql)) { return false; }
    		if(!eregi("^select",$sql))
    		{
    			echo "<H2>Wrong function silly!</H2>\n";
    			return false;
    		}
    		if(empty($this->CONN)) { return false; }
    		$conn = $this->CONN;
    		$results = mysql_query($sql,$conn);
    		if( (!$results) or (empty($results)) ) {
    			mysql_free_result($results);
    			return false;
    		}
    		$count = 0;
    		$data = array();
    		while ( $row = mysql_fetch_array($results))
    		{
    			$data[$count] = $row;
    			$count++;
    		}
    		mysql_free_result($results);
    		return $data;
    	}
    
    	function insert ($sql="")
    	{
    		if(empty($sql)) { return false; }
    		if(!eregi("^insert",$sql))
    		{
    			echo "<H2>Wrong function silly!</H2>\n";
    			return false;
    		}
    		if(empty($this->CONN))
    		{
    			echo "<H2>No connection!</H2>\n";
    			return false;
    		}
    		$conn = $this->CONN;
    		$results = mysql_query($sql,$conn);
    		if(!$results) 
    		{
    			echo "<H2>No results!</H2>\n";
    			echo mysql_errno().":  ".mysql_error()."<P>";
    			return false;
    		}
    		$results = mysql_insert_id();
    		return $results;
    	}
    
    	function sql_query ($sql="")
    	{
    		if(empty($sql)) { return false; }
    		if(empty($this->CONN)) { return false; }
    		$conn = $this->CONN;
    		$results = mysql_query($sql,$conn);
    		if(!$results) 
    		{
    			echo "<H2>Query went bad!</H2>\n";
    			echo mysql_errno().":  ".mysql_error()."<P>";
    			return false;
    		}
    		return $results;
    	}
    
    	function sql_cnt_query ($sql="")
    	{
    		if(empty($sql)) { return false; }
    		if(empty($this->CONN)) { return false; }
    		$conn = $this->CONN;
    		$results = mysql_query($sql,$conn);
    		if( (!$results) or (empty($results)) ) {
    			mysql_free_result($results);
    			return false;
    		}
    		$count = 0;
    		$data = array();
    		while ( $row = mysql_fetch_array($results))
    		{
    			$data[$count] = $row;
    			$count++;
    		}
    		mysql_free_result($results);
    		return $data[0][0];
    	}
    Any suggestions?

  7. #7
    SitePoint Member
    Join Date
    Mar 2001
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Don't know whether this will help you but here goes:

    You are using the mysql_query command which means that maybe you will have to use
    $variable=mysql_fetch_array($results)
    Now variable contains the first of the rows of the database that have been selected. Any field of that particular row can be accessed as $variable["field"]. You can put the fetch array command in a while loop and when it evaluates to false it means that all results have been obtained.Then you can come out of the while loop. Inside the while loop you can give the operations to be performed with a particular result of the query.
    eg: If database contains info of 10 boys and your query satisfies the info of 5 boys thaen reults will contain info of 5 boys. With fetch array command variable will contain info of 1st selected boy, the next ftech array will assign info of 2nd selected boy and so on.

    PS: I logged in just now and I could see the password that I had typed instead of asterisks. Hope its just a temp. problem.

  8. #8
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well firstly, my programming experience is in strongly typed languages such as C++ and java. The idea of having a function return a false (bool) in one path and an array through another path freaks me out!but probably is not so bad as it seams. Anyway, that is irrelevent to your problem.

    Well the only practical suggestion I can give is to comment (or delete) this line from the two functions.

    mysql_free_result($results);

    However, that execution enters the if block as it did for you when you received this error message; and the fact that this code has been released to the public (so I assume it was "tested" to some degree in that it must have worked for the original programmer); suggests that you have an unexpected error in the sql string that is being passed to the function (or there is a problem with your database connection - but you say it should be fine).

    Perhaps the illogic of the mysql_free_result() call was not picked up by the original programmer because the if block was never entered when he/she was testing it. This also suggests to me that this code was not properly unit tested before released. That and the fact that I find these functions poorly designed suggests that this code should not be rellied apon "out of the box"

    So I would do two things. First comment out the call to mysql_free_result() and second debug the sql string that you are passing to the function when this error occurs.

    Good luck.

  9. #9
    [Call me Bram] iBram007's Avatar
    Join Date
    Feb 2001
    Location
    Belgium
    Posts
    339
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've contacted the author, here's the answer:
    Simple bug, I was trying to do the impossible. Fix posted many moons ago:

    http://www.thewebmasters.net/phpHoo/


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
  •