Jump to content

fatal error: call to a member function


CyberShot

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();
	}
		
	}

}

 

 

Link to comment
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".

Link to comment
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.

Link to comment
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.
Link to comment
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. 

 

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.