Jump to content

php-beginner

Members
  • Posts

    59
  • Joined

  • Last visited

    Never

Everything posted by php-beginner

  1. Ok, Thankyou for your patience and solving my problem. I still have one question open . Why do i have to return the value in the example given? public function connect() { if(is_null($this->connection)) { $this->connection = new mysqli('localhost', 'root', '', 'login'); } return $this->connection;
  2. It isn't, I just extended upon your example. Ah ok. But i don't want to store all methods in one class because then it is not object oriented right? So, why is this necessary in my example?
  3. No, a class is a container of variables and functions. I could establish the DB connection in the constructor and all methods would have access to the DB connection. Then why is it necessary in this case? (sorry for the lame questions)
  4. Thanks! So I always have to return the value that i have created for another variable?
  5. Hello everyone, For two weeks now, I'm trying to get this database connection in my query. Can someone give me a solution and tell me what I've done wrong? Am I overlooking something? <?php class Mysql{ public function connect(){ $mysqli = new mysqli('localhost','root','','login'); } } class Query extends Mysql{ public function runQuery(){ $this->result = parent::connect()->query("select bla bla from bla bla"); } } $query = new Query; $query->runQuery(); ?>
  6. Yes, that is exactly what i need. But this won't work right? $this->mysql->query($query);
  7. So far, thankyou for your reactions. Unfortionatly i can't get my script to work. I know what i am doing wrong, but i can't solve this myself: <?php class Mysql{ public function __construct(){ $mysqli = new mysqli('localhost','root','','login'); } } class Query extends Mysql{ public function loginQuery(){ $query = "SELECT gebruikerid FROM gebruikers WHERE gebruikersnaam = 'wouter' AND wachtwoord = 'test'"; } } class User{ public function __construct(){ $this->mysql = new Mysql(); } public function login(){ $this->mysql->query($query); } } ?>
  8. But, this way I am calling login() from the Database class right? Gives me: Fatal error: Call to undefined method Database::login()
  9. Hello everyone, I'd like to make a small object oriented login system. The problem is that I'm not very good at oop and i have only written these scripts the procedural way. So please, correct me if I'm wrong: class database - connect() class user - login() My problem is when i make a database connection in connect(), I can't use it in login(). class Database{ public function connect(){ $mysqli = new mysqli('localhost','root','','login'); } } class User{ public function login(){ // how do i use connect() from above and make a query to log the user in? }
×
×
  • 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.