CyberShot Posted December 10, 2018 Share Posted December 10, 2018 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(); } } } Quote Link to comment Share on other sites More sharing options...
benanamen Posted December 10, 2018 Share Posted December 10, 2018 (edited) 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 December 10, 2018 by benanamen Quote Link to comment Share on other sites More sharing options...
CyberShot Posted December 10, 2018 Author Share Posted December 10, 2018 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 Quote Link to comment Share on other sites More sharing options...
requinix Posted December 10, 2018 Share Posted December 10, 2018 I don't suppose you mis-copied the bit of code about where the error message comes from? Quote Link to comment Share on other sites More sharing options...
Barand Posted December 10, 2018 Share Posted December 10, 2018 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. Quote Link to comment Share on other sites More sharing options...
requinix Posted December 10, 2018 Share Posted December 10, 2018 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. Quote Link to comment Share on other sites More sharing options...
Barand Posted December 10, 2018 Share Posted December 10, 2018 Yeah, there are a couple of things wrong with the line that does contain the error The variable doesn't exist, it should be referenced via $this 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. Quote Link to comment Share on other sites More sharing options...
CyberShot Posted December 11, 2018 Author Share Posted December 11, 2018 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.