mysql_query gives Resource id #17

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • KeredDrahcir
    Contributor
    • Nov 2009
    • 426

    #1

    mysql_query gives Resource id #17

    Have a table that needs to have results written to it, that will later be updated. To check if the table has been written to yet I'm using this code:
    Code:
        $sql="SELECT DISTINCT [I]field[/I] FROM [I]table[/I] WHERE [I]field[/I]=".$[I]value[/I];
        $new_result=mysql_query($sql,$db);
        if (!$new_result)
        {
          [I]do xyz[/I]      
        }
    When I do this it doesn't enter the if. I tried echoing $new_results and got Resource id #17. What does this mean and what am I doing wrong that prevents me from entering the if. I tried the sql statement in an sql command prompt and it gave me Empty set (0.00 sec), which I was hoping for.
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    From the manual
    Code:
    For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error
    Does this clear things up?

    Comment

    • KeredDrahcir
      Contributor
      • Nov 2009
      • 426

      #3
      Not really. It's returning a Rescource on sucess but as I mentioned it was getting Empty Set (which I wanted) in the sql so surely it should be returning false?
      Am I missing something?

      Comment

      • code green
        Recognized Expert Top Contributor
        • Mar 2007
        • 1726

        #4
        Yes, an empty result set is not an error.
        To check for an empty result set use mysql_num_rows( )

        Comment

        • KeredDrahcir
          Contributor
          • Nov 2009
          • 426

          #5
          Thanks. I did figure it out. I don't understand, that for some reason it did work to begin with and then now it suddenly stopped working.
          I've got it woring now. I used mysql_get_row first and then checked it. Is mysql_num_rows a better function to use or are they both much the same?

          Comment

          • code green
            Recognized Expert Top Contributor
            • Mar 2007
            • 1726

            #6
            Entirely different.
            mysql_get_row() Returns one row from the resultset.
            It is generally used inside a loop to retrieve records.

            mysql_num_rows( ) returns a count of the number of rows in the resultset.
            It is generally used to check how many records the query found.

            All this stuff is freely available in the PHP manual.
            Download it and and use a link via your desktop for easy access

            Comment

            • KeredDrahcir
              Contributor
              • Nov 2009
              • 426

              #7
              Thanks. I know the difference between to functions. My code shows that I'm trying the check for a result. I was just wondering is using mysql_get_row() and then if (!$result) was better or worse than checking with mysql_num_rows( )?

              Comment

              • code green
                Recognized Expert Top Contributor
                • Mar 2007
                • 1726

                #8
                Thanks. I know the difference between to functions
                You may understand the functions but you are not using them the way nature intended.
                By the way it is mysql_fetch_row not mysql_get_row() .
                mysql_get_row() and then if (!$result) was better or worse than checking with mysql_num_rows( )?
                mysql_fetch_row () takes the result resource as a parameter.
                If there is no result resource it cannot return a row. So your suggestion is meaningless.
                It is much better to use the right function for the right job in the right order.
                Code:
                if(false===($result = mysql_query($query,$resource))) error
                if(mysql_num_rows($result)
                {
                 while($record = mysql_fetch_row($result))
                }
                Or something like that.
                I apologise for any syntax errors.

                Comment

                • KeredDrahcir
                  Contributor
                  • Nov 2009
                  • 426

                  #9
                  I copied mysql_get_row() from your post.

                  Code:
                  $new_result_test=mysql_query($sql,$db);
                  $new_result = mysql_fetch_row($new_result_test);
                  if (!$new_result)
                  {
                    new coffee();
                  }
                  This is what I have found to work. I was trying to find out if there was a better way of doing it. Am I doing something wrong there?

                  What I think I'm doing is running the query to try and get a row from the table. I'm doing that to check if a row with a specified value exists yet becuase if it doesn't I need to create it.
                  Is the method I've used stable?

                  Comment

                  • code green
                    Recognized Expert Top Contributor
                    • Mar 2007
                    • 1726

                    #10
                    I think I understand what you are trying to achieve now.
                    If you simply want to know if a certain record exists, but you don't want to do anything with it,
                    the most efficient method would be
                    Code:
                    $sql="SELECT 1 FROM table WHERE field=".$value; //Will return first field only
                    $new_result_test = mysql_query($sql,$db); 
                    if(!mysql_num_rows($new_result_test); 
                    { 
                      new coffee(); 
                    }
                    There is no need to return any rows from the resultset if you are not going to use them.
                    Remember mysql_query() will still return a resource if no rows are found so testing the resource only proves the query executed,
                    which is where I think you are getting confused.

                    Comment

                    • KeredDrahcir
                      Contributor
                      • Nov 2009
                      • 426

                      #11
                      Thanks. If I do need to use the result, am I still better of testing mysql_num_rows or testing the result?

                      Comment

                      • code green
                        Recognized Expert Top Contributor
                        • Mar 2007
                        • 1726

                        #12
                        Again, use the functions for the purpose intended.
                        There is no result to test, there is only the function return values
                        Code:
                        //mysql_query() returns a resource on success, or FALSE on error 
                        $result = mysql_query($query,$resource);
                        if($result != false)
                        {
                        if(mysql_num_rows($result) > 0) 
                        //returns a count of the number of rows in the resultset.
                        {
                        /*mysql_fetch_row() 
                          Returns a numerical array that corresponds to the fetched row, 
                          or FALSE if there are no more rows. */ 
                         while($record = mysql_fetch_row($result)) 
                        }  
                        }

                        Comment

                        Working...