How to solve this Warning: mysql_fetch_object():

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
jaggu2003Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hernst42Commented:
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
RoonaanCommented:
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
minichickenCommented:
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
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

jaggu2003Author Commented:
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
hernst42Commented:
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
jaggu2003Author Commented:
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
jaggu2003Author Commented:
please help  me to get sort out this issue. Hernst2 anything else u require to solve this.

Thanks in advance
jaggu
0
RoonaanCommented:
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
hernst42Commented:
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
jaggu2003Author Commented:
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
minichickenCommented:
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
jaggu2003Author Commented:
Minichiken,

Same error. :(

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

Thanks in advance
Jaggu
0
RoonaanCommented:
You cannot upload, just copy and past will do.

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

-r-
0
jaggu2003Author Commented:
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
RoonaanCommented:
I get totally no errors on this code...

-r-
0
RoonaanCommented:
Possibly you have to change the ########## blocks into blocks with /* */ but this seems very unlikely to me.

-r-
0
Bernard S.CTOCommented:
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
jaggu2003Author Commented:
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
RoonaanCommented:
The first answer (by hernst42) is the answer to the "main Question".

-r-
0
Bernard S.CTOCommented:
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
jaggu2003Author Commented:
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
Bernard S.CTOCommented:
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
moduloCommented:
PAQed with points refunded (50)

modulo
Community Support Moderator
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.