Jump to content

[SOLVED] Call to a member function query() on a non-object in {FILEPATH}


newbiz

Recommended Posts

Hi,

 

Can someone help with this error.  I have researched here and on the web and it seems as tho there are alot of reasons for getting this error, most which stem from not having a database connection?? In my code below everything executes fine until the SELECT statement where i get a Fatal error: Call to a member function query() on a non-object in {FILEPATH} errorb] error. I have taken the SELECT query out and the data has been passed into the database,therefore i know there is a databse connection.  Is there any other reason this error is apprearing?

 

<?php

  if(!$conn = mysql_connect($host,$user,$passwd)) die ("Unable to connect to database");

//Select MySQL DB 
  if(!mysql_select_db($db,$conn)) die ("Unable to select database $db");

$quote = $_POST['quote'];
$text = $_POST['text'];
$image = $_POST['image'];

$page = $_GET['page'];


$escimage = mysql_real_escape_string($image);
$esctext = mysql_real_escape_string($text);
$escquote = mysql_real_escape_string($quote);

mysql_query("UPDATE content SET image = '$escimage', text = '$esctext', quote = '$escquote'
WHERE page = '$page'");

//Following error throws Fatal error: Call to a member function query() on a non-object in {FILEPATH} error
$sql = 		"SELECT * FROM content 
   		WHERE page = '$thisPage'";
		$result = $db->query($sql);
?>

Link to comment
Share on other sites

Hi,

 

Can someone help with this error.  I have researched here and on the web and it seems as tho there are alot of reasons for getting this error, most which stem from not having a database connection?? In my code below everything executes fine until the SELECT statement where i get a Fatal error: Call to a member function query() on a non-object in {FILEPATH} errorb] error. I have taken the SELECT query out and the data has been passed into the database,therefore i know there is a databse connection.  Is there any other reason this error is apprearing?

 

<?php

  if(!$conn = mysql_connect($host,$user,$passwd)) die ("Unable to connect to database");

//Select MySQL DB 
  if(!mysql_select_db($db,$conn)) die ("Unable to select database $db");

$quote = $_POST['quote'];
$text = $_POST['text'];
$image = $_POST['image'];

$page = $_GET['page'];


$escimage = mysql_real_escape_string($image);
$esctext = mysql_real_escape_string($text);
$escquote = mysql_real_escape_string($quote);

mysql_query("UPDATE content SET image = '$escimage', text = '$esctext', quote = '$escquote'
WHERE page = '$page'");

//Following error throws Fatal error: Call to a member function query() on a non-object in {FILEPATH} error
$sql = 		"SELECT * FROM content 
   		WHERE page = '$thisPage'";
		$result = $db->query($sql);
?>

What is this?

$result = $db->query($sql);

i think you are using some custom class, have you included in the code?? if you're using the class, which class are you using?? Its likely you have called wrong method in a class.

Link to comment
Share on other sites

"$db->query()" is a method from a class, it's not standard PHP. You've either forgotten to include it or trying to replicate code you've seen else where. If you use the standard PHP function mysql_query() it will correct the error, or include the DB class.

Link to comment
Share on other sites

Thanks for the replys.

<?php
  // Include MySQL class
include('../../includes/mysql.class.php');
?>

Above I have included the MySQL class at the top of the file. The MySQL class works in other parts of the site using the same function $result = $db->query($sql);. It is definetly pointing to the file otherwise i would get an error saying the files does not exist.

 

So there seems to be a problem with making a call to the function in this instance, even though the function is the same and the query is the same.  The only difference between this and a file that is working correctly is they are different files, and one file is one level below the other.

Link to comment
Share on other sites

That may not neccesarily be the case, a call to an include that didn't exist would thrown a warning, not a fatal error. Try using require. Your errors may setup to only show fatal errors. Although, do you realise the code you posted originally does not contain the include?

Link to comment
Share on other sites

I scrapped the custom MySQL class and reverted back to standard php and got it all working. the reason i persisted with the custom class was because i had other functions used in the file that accessed the class.  I cant for the life of me figure out why this is the only file that cant access the functions.  But got it all working, so thanks for the replys.

Link to comment
Share on other sites

I scrapped the custom MySQL class and reverted back to standard php and got it all working. the reason i persisted with the custom class was because i had other functions used in the file that accessed the class.  I cant for the life of me figure out why this is the only file that cant access the functions.  But got it all working, so thanks for the replys.

then you might have called wrong method of a class to execute select query.

 

in custom class, there might method get_var or get_results or get_row or something like that, so if these methods exist you should use like $db->get_row($sql)

Link to comment
Share on other sites

i copied the code from another other page that called the same methods, and changing the code to the corresponding standard php methods fixed everything (the variables were correct). So im guessing it must have had something to do with my inclusion of the custom class..

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.