CyberShot Posted December 18, 2010 Share Posted December 18, 2010 I am trying to set up a class for my database connection. I have it working one way, this is a completely new method I am trying to learn. I have a file called MyClasses.php and in that file I did this. (I am following a not very well done tutorial) class MySQLDatabase { private $connection; function __construct(){ $this->open_connection(); } public function open_connection(){ $this->connection = new MySQLi('localhost','MyDatabase','password','billpay') or die($mysql->error); } } $database = new MySQLDatabase(); then in my index page where I want to begin by doing a query on the database, I did this <?php include MyClasses.php ?> $result = $database->query("SELECT * FROM names") or die($mysql->error); but that gives me this error Fatal error: Call to undefined method MySQLDatabase::query() in C:\wamp\www\BillPay\index.php I can't figure out how to get past it. Quote Link to comment Share on other sites More sharing options...
Anti-Moronic Posted December 18, 2010 Share Posted December 18, 2010 Does the tutorial use query() method? It is because you are either a) not extending any other classes and so not inheriting any other functionality, or b) do not have query() method defined in your current class. Quote Link to comment Share on other sites More sharing options...
CyberShot Posted December 18, 2010 Author Share Posted December 18, 2010 I thought query was a function of it's own. I don't know how to fix it Quote Link to comment Share on other sites More sharing options...
Anti-Moronic Posted December 18, 2010 Share Posted December 18, 2010 No, you will have to do this: class MySQLDatabase { private $connection; function __construct(){ $this->open_connection(); } public function open_connection(){ $this->connection = new MySQLi('localhost','MyDatabase','password','billpay') or die($mysql->error); } public function query($query){ return $this->connection->query($query); } } I'm not really sure this is the best way to be interacting with the database. You might consider downloading a decent library which already has this setup for you. Quote Link to comment Share on other sites More sharing options...
trq Posted December 18, 2010 Share Posted December 18, 2010 Why are you writing a wrapper around an already perfectly good database class MySqli ? Your class adds nothing of value. Quote Link to comment Share on other sites More sharing options...
CyberShot Posted December 18, 2010 Author Share Posted December 18, 2010 I didn't realize that is what I was doing. I think I see what you mean. So I should not use the mysqli inside the class. How do I make this work? Quote Link to comment Share on other sites More sharing options...
Anti-Moronic Posted December 18, 2010 Share Posted December 18, 2010 The main reason for using mysqli inside this class is to abstract the database access layer. If you don't require that, then it is simply a case of managing the database connection effectively. Should note that simply having this as it is, is hardly considered abstraction. The only use is it contains a central place for database connection settings. Some simple documentation on mysqli here: http://www.php.net/manual/en/mysqli.query.php 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.