Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to solve this Warning: mysql_fetch_object():

Posted on 2004-10-27
25
Medium Priority
?
400 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
Comment
Question by:jaggu2003
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 6
  • 3
  • +3
25 Comments
 
LVL 48

Expert Comment

by:hernst42
ID: 12420959
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
ID: 12421156
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
ID: 12421230
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:jaggu2003
ID: 12422179
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
ID: 12422274
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
ID: 12422406
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
ID: 12432127
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
ID: 12432225
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
ID: 12432299
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
ID: 12432464
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
ID: 12432868
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
ID: 12432997
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
ID: 12433072
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
ID: 12433130
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
ID: 12433229
I get totally no errors on this code...

-r-
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 12433243
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
ID: 12448285
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
ID: 12455962
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
ID: 12456191
The first answer (by hernst42) is the answer to the "main Question".

-r-
0
 
LVL 29

Expert Comment

by:fibo
ID: 12458298
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
ID: 12501923
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
ID: 12868394
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:
modulo earned 0 total points
ID: 12905720
PAQed with points refunded (50)

modulo
Community Support Moderator
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

618 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