Solved

How to solve this Warning: mysql_fetch_object():

Posted on 2004-10-27
373 Views
Last Modified: 2006-11-17
Hi All,

Am new to PHP programming, I have Alumni website in that am setting FORUM application. For that I have downloaded some ready code from Net. Now Am getting error;
----------------------------------------
Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in /home/demo_test/public_html/Alumni-Demo/sql/MySQL.php on line 111
----------------------------------------------------------

Can you please help me to get sort out this problem. Please advise me wht to change in code. I have referred Google to get solution but i couldn't able to do so.  

Following are the Functions which am using ;
--------------------------
function num_rows($res)
 {
  $rows=mysql_fetch_row($res);
  return $rows;
 }    


 function fetch_object($res,$nr)
 {                                    
  $row = mysql_fetch_row $res);
 if($row) return $row;
 else return false;
}
---------------------------------------

I appreciate your time & inputs.

Thanks
Jaggu Joshi
0
Question by:jaggu2003
    23 Comments
     
    LVL 48

    Expert Comment

    by:hernst42
    Are you sure you have entered the correct database settings or do you have executed a SELECT-Statement before that function mysql_fetch_object() is called.
    The warning indicates that either there was no query run before or the query that was run got an SQL-error.
    0
     
    LVL 49

    Expert Comment

    by:Roonaan
    As hernst42 says, indeed the most likely problem will be an SQL error in the query that was executes. (Or actually, was not executed :-) ). Therefor easiest would be to track the mysql_query function and see if it is handled well in terms of error_handling and error reporting. (mysql_error() proves to be a good companion in such cases)

    Regards

    -r-
    0
     
    LVL 12

    Expert Comment

    by:minichicken
    Hi

    Don't know if this is the problem, but you missing an opeing bracket near >>  $row = mysql_fetch_row $res);

    Here is the code with the bracket....
    *********************************************************
    function num_rows($res)
     {
      $rows=mysql_fetch_row($res);
      return $rows;
     }    


     function fetch_object($res,$nr)
     {                                    
      $row = mysql_fetch_row ($res);
     if($row) return $row;
     else return false;
    }
    0
     

    Author Comment

    by:jaggu2003
    Thank you, for your reply. hernst42 & Roonaan i think ur right ... I didnt mentioned DB connection in this file but am confusing that whr exact I have to mention. Here is the code. May be its so simple but am struggling, ;)
    ----------------------------------------------------------------------------------------------

    class Sql
    {
     var $dbtype;      
     var $host;            
     var $user;            
     var $password;      
     var $db;              
     var $dbid;    
     
     function Sql($hst, $usr, $passwd, $dbase)
     {
      $this->host=$hst;
      $this->user=$usr;
      $this->password=$passwd;
      $this->db=$dbase;
      $this->connect();
     }
     
     function connect()
     {
      $this->dbid=mysql_connect($this->host, $this->user, $this->password);
      mysql_select_db($this->db);
     }
    ----------------------------------------------------------------------------------------------

    I have tried to mention DB details in All functions but its falling. Especially, I have mentioned in "Function SQL" its giving error.
    Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `'{'' or `'$'' in /home/kashmirn/public_html/Alumni-Demo/sql/MySQL.php on line 31

    Thanks once again, for your inputs
    Jaggu Joshi

    0
     
    LVL 48

    Expert Comment

    by:hernst42
    Can you post line 31 of that file or a url to view the source of that php-file
    cp the MySQL.php to MySQL.phps so the source is viewable.
    0
     

    Author Comment

    by:jaggu2003
    Here is error Message
    -----------------------------
    Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `'{'' or `'$'' in /home/kashmirn/public_html/Alumni-Demo/sql/MySQL.php on line 33
    -----------------------------

    Here is Code
    ----------------------------------
    29     function Sql($host, $user, $passwd, $dbase)
    30     {
    31         $this->localhost=$host;
    32         $this->userDB=$user;
    33         $this->test=$passwd;
    34         $this->DB_online=$dbase;
    35         $this->connect();
    36     }
    ----------------------------------

    FYI- password is correct its using in another place there its working fine.
    :)

    0
     

    Author Comment

    by:jaggu2003
    please help  me to get sort out this issue. Hernst2 anything else u require to solve this.

    Thanks in advance
    jaggu
    0
     
    LVL 49

    Expert Comment

    by:Roonaan
    The line 29-36 contain no erros as far as I can tell. One non-syntax error however could be in the lines:

     function connect()
     {
      $this->dbid=mysql_connect($this->host, $this->user, $this->password);
      mysql_select_db($this->db);
     }

    You first assign connection to $this->dbid and then reference to $this->db when selecting the database. This will always fail I think. And could be the reasons why your queries fail.

    Regards

    -r-
    0
     
    LVL 48

    Expert Comment

    by:hernst42
    I also don't see any errors in that lines

    mysql_select_db($this->db); is correct. Is the same as
    mysql_select_db($this->db, $this->dbid);
    0
     

    Author Comment

    by:jaggu2003
    Guys ,

    Again same error throwing me

    here is error message
    -------------------------------------
    Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `'{'' or `'$'' in /home/kashmirn/public_html/Alumni-Demo/sql/MySQL.php on line 45
    -------------------------------------

    Code is here
    ------------------------------------
    43    function connect()
    44    {
    45         $this->dbid=mysql_connect($this->"localhost", $this->"userDB", $this->"test");
    46         mysql_select_db($this->"DB_online");
    47     }
    ------------------------------------
    0
     
    LVL 12

    Expert Comment

    by:minichicken
    Take out quotes? (") ??

    43    function connect()
    44    {
    45         $this->dbid=mysql_connect($this->localhost, $this->userDB, $this->test);
    46         mysql_select_db($this->DB_online);
    47     }
    0
     

    Author Comment

    by:jaggu2003
    Minichiken,

    Same error. :(

    Would u like to see code of full page ? how can u upload here?

    Thanks in advance
    Jaggu
    0
     
    LVL 49

    Expert Comment

    by:Roonaan
    You cannot upload, just copy and past will do.

    Check that you remove any unwanted passwords from your code, before posting!

    -r-
    0
     

    Author Comment

    by:jaggu2003
    Guys here is code of Full page i.e. MySQL.PHP  Hope this will help you to debug.
    ------------------------------
    <?php
    if (eregi("mysql.php",$_SERVER["PHP_SELF"]))
    {
     Header("Location: ../");
     die();
    }

    class Sql
    {
     var $dbtype;      
     var $host;            
     var $user;            
     var $passwd;      
     var $db;              
     var $dbid;    
     
    #####################################################
    # Construction function                                                #
    # Sets Sql host, user, password and db                        #
    #####################################################
     
     function Sql($host, $user, $passwd, $dbase)
     {
      $this->host=$hst;
      $this->user=$usr;
      $this->password=$passwd;
      $this->db=$dbase;
      $this->connect();
     }
     
    #######################################################################
    #           connect()                                                                        #
    # connects to the database and saves connection id in $id variable    #
    #######################################################################

     function connect()
     {
      $this->dbid=mysql_connect($this->localhost, $this->userDB, $this->test);
      mysql_select_db($this->DB_online);
     }

    ##############################################################
    # Logout the user from database (close database connection)  #
    ##############################################################
     
     function logout()
     {
      $this->dbid=mysql_close($this->dbid);
     }


    ##########################################################
    #               query($query)                               #
    # executes an SQL statement, returns a result identifier #
    ##########################################################
     
     function query($query)
     {
      $res=mysql_query($query, $this->dbid);
      return $res;
     }      
           
    ####################################################################
    #               num_rows($res)                                                            #
    # given a result identifier, returns the number of affected rows   #
    ####################################################################

     function num_rows($res)
     {
      $rows=mysql_fetch_row($res);
      return $rows;
     }                                    
                               
    #######################################################################
    #                  fetch_row($res,$row)                                   #
    # given a result identifier, returns an array with the resulting row  #
    #######################################################################                                    
     function fetch_row($res, $nr)    
     {                                    
      $row = mysql_fetch_row($res);
      return $row;
     }                                    
                                         
    ############################################################
    #            fetch_array($res,$row)                             #
    # given a result identifier, returns an associative array  #  
    # with the resulting row using field names as keys.        #  
    ############################################################
                                         
     function fetch_array($res,$nr)  
     {                                    
      $row = array();              
      $row = mysql_fetch_array($res);
      return $row;                
     }

    ############################################################
    #
    # returns an object with field names as data members
    #
    ############################################################
     function fetch_object($res,$nr)
     {                                    
      $row = mysql_fetch_row ($res);
     if($row) return $row;
     else return false;
    }
    }
    ?>
    ----------------------------------------------------------


    Please try to to get solution for this,.
    Jaggu Joshi
    0
     
    LVL 49

    Expert Comment

    by:Roonaan
    I get totally no errors on this code...

    -r-
    0
     
    LVL 49

    Expert Comment

    by:Roonaan
    Possibly you have to change the ########## blocks into blocks with /* */ but this seems very unlikely to me.

    -r-
    0
     
    LVL 29

    Expert Comment

    by:fibo
    You might consider getting more debug info and checking it.
    Since I very often get an message similar to the first one you get? I've now reached the convistion that in 99.9% of cases the text of my sql query s wrong...
    To be able to check if this is true, I usually do the following:
    1 - Launch phpMyAdmin, connect to the database
    2 - in tyhe php program [I think in your query() code), insert something like
        if ($debug ==1 ) { echo "SQL Query is :$query:<brt>\n";};
    3 - Run the program. If yoy get the usual message, copy the sql query code from the page between the : :, paste it into the "sql query box" in phpMyAdmin, and run it.
    This will usually give you more info about what is going wrong...
    0
     

    Author Comment

    by:jaggu2003
    FIBO i tried as u mentioned above, but i didnt get any error in query..

    Is there any specific answer for this main Question.
    Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource

    Thanks ur inputs,
    jaggu joshi
    0
     
    LVL 49

    Expert Comment

    by:Roonaan
    The first answer (by hernst42) is the answer to the "main Question".

    -r-
    0
     
    LVL 29

    Expert Comment

    by:fibo
    Hi,
    It seems that the "sql resource" you are using (implicitly in your case), ie the db_link, is not a correct value.

    1 - doubly check this ressource, being careful thet "$res" might be browsed upon both as "result" and "resource".

    2 - if nothing appears here, check if your sql resource gets a value in THIS php page (it cannot be transferred from page to page, you need to reponde the db in each page).

    3 - if still no light appears, I would try to explicitly use the functions mysql_ withh the sql ressource explicit.

    Good bug-hunt!

    B.
    0
     

    Author Comment

    by:jaggu2003
    Hi Guys,

    Finally, I am able to execute my query, without changing anything in code. My collegue has advised me this.

    Its  interesting actually, my Tables were  empty!. I added one record manually, then Its all fine!

    I really dont no wht was the problem, even if record is not there then that should display Blankl HTML Table.

    Thanks for all ur help !
    Jaggu Joshi
     
    0
     
    LVL 29

    Expert Comment

    by:fibo
    well,
    since so many people did try to solve the problem, and the cause of the problem was somewhere else, I would support that this get PAQed, maybe with a headline addition like "empty tables".
    0
     

    Accepted Solution

    by:
    PAQed with points refunded (50)

    modulo
    Community Support Moderator
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
    Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    877 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now