Jump to content
CyberShot

fatal error: call to a member function

Recommended Posts

I am trying to create a class and I ran into a problem. The connect function is returning a fatal error on the line below and I can't figure out the issue.

$result = $this->conn->query("SELECT unit FROM address");

 

class database {
	
	private	$server = "localhost";
	private	$db_user = "my user"; 
	private	$db_pass = "my pass"; 
	private	$db_name = "my database"; 
    private $conn;
	
	private $result = array(); 


	function connect(){
		// Create connection
		$this->conn = new mysqli($this->server, $this->db_user, $this->db_pass, $this->db_name);
		
		if ($this->conn->connect_error) {
			echo "Not connected, error: " . $mysqli_connection->connect_error;
		}
		else {
			//echo "Connected.";
			return $this->conn;
		}

	public function displayHeader(){
		$result = $this->conn->query("SELECT unit FROM address");

		if ($result->num_rows > 0) {
			// output data of each row
			while($row = $result->fetch_assoc()) {
				echo '<li>' . $row["unit"] . '</li>';
			}
		} else {
			echo "0 results";
		}
		$conn->close();
	}
		
	}

}

 

 

Share this post


Link to post
Share on other sites

Your curly braces are messed up. If you used a proper IDE you would see these basic problems. There are other issues as well including trying to call a private property. Overall, this is a pretty bad Class. I will let the other members comment further.

EDIT: couple more comments: The connection parrameters should not be hard coded in the class. You should not be outputting internal system errors to the users. The querys should be passed, not hard coded nor the HTML, the connection should do just that, with the CRUD operations in another class. The path you are on will require you to always have to edit the class every time you use it. Think "Single Responsibility".

Edited by benanamen

Share this post


Link to post
Share on other sites

I see in my post that the braces are messed up. I just didn't copy the code correctly. They are not messed up in the class

Share this post


Link to post
Share on other sites

I don't suppose you mis-copied the bit of code about where the error message comes from?

Share this post


Link to post
Share on other sites

Talking of error messages, after three hundred posts you should have learned that is not sufficient just to say you have "an error". Provide us with the actual error message - they are provided to give clues about what the error might be.

Share this post


Link to post
Share on other sites
1 minute ago, Barand said:

Talking of error messages, after three hundred posts you should have learned that is not sufficient just to say you have "an error". Provide us with the actual error message - they are provided to give clues about what the error might be.

It's the title: call to a member function on a non-object. The sad thing is that it should even point out the exact line where the problem is, which is just about as far as anyone can go to saying what the problem is without actually stating it.

Share this post


Link to post
Share on other sites

Yeah, there are a couple of things wrong with the line that does contain the error

  1. The variable doesn't exist, it  should be referenced via $this
  2. The call shouldn't be there at all. One certainly shouldn't close the connection after each query and it's seldon necessary ever to close a connection. It is done automatically when the script ends.

Share this post


Link to post
Share on other sites
8 hours ago, requinix said:

It's the title: call to a member function on a non-object. The sad thing is that it should even point out the exact line where the problem is, which is just about as far as anyone can go to saying what the problem is without actually stating it.

this is true that the error does give you the line number but it isn't true that the line it points out is always the problem. It could be 2 or 3 lines above the actual line pointed out. I have seen this before. That said, I do not have any formal programming schooling. I am trying to teach myself which is not as easy as it sounds. There are lots of tutorials out there but they are notorious for leaving out information. They do things but do not tell you why they did them. I just finally came to understand the use of "$this->" as of yesterday. 

 

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

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.