Jump to content

php-beginner

Members
  • Posts

    59
  • Joined

  • Last visited

    Never

Posts 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. Then why is it necessary in this case? (sorry for the lame questions)

     

    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. Thanks!

     

    So I always have to return the value that i have created for another variable?

     

    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. 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();
    
    ?>

  5. I have no idea how to use mysqli as I use mysql but if they have the same principals, is this what your looking for?

     

    <?php
    
    class Mysql {
    
    public function __construct() {  
    	$mysqli = new mysqli( 'localhost', 'root', '', 'login' );
    }
    
    }
    
    class query extends Mysql {
    
    private $query;
    private $item;
    
    public function __construct($query) {
    	$queryRun = $this->executeQuery($query);
    	if($queryRun) {
    		return true;
    	} else {
    		return $queryRun;
    	}
    }
      
    public function executeQuery($query) {
    	$this->queryReturn = mysqli_query($query);
    
    	if($this->queryReturn) {
    		return true;
    	} else {
    		return mysqli_error();
    	}
    }
    
    public function returnNumRow() {
    	return mysqli_num_rows($this->queryReturn);
    }
    
    public function returnWhile($item) {
    	while($row = mysqli_fetch_assoc($this->queryReturn)) {
    		return $row[$item];
    	}
    }
    }
    
    class User {
      
    public function __construct() {
    	$this->mysql = new Mysql();
    }
      
    public function login() {
    	$query = "SELECT gebruikerid FROM gebruikers WHERE gebruikersnaam = 'wouter' AND wachtwoord = 'test'";
    	$this->mysql->query($query);
    
    	if($this->mysql->returnNumRow() == 1) {
    		echo $this->mysql->returnWhile('username');
    	} else {
    		return 'Your account does not exist';
    	}
    }
    }
    
    ?>
    

     

    James.

     

    Yes, that is exactly what i need. But this won't work right?

     

    $this->mysql->query($query);

  6. 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);
    }
    
    }
    
    ?>

  7. You need to make the database connection a member variable of the user class:

    class User
    {
      public function __construct()
      {
        $this->db = new Database();
        $this->db->login();
      }
    }

    Also: the way I've described here is inefficient, look into the singleton pattern and instanced classes in PHP.

     

    -Dan

     

    But, this way I am calling login() from the Database class right? Gives me: Fatal error: Call to undefined method Database::login()

  8. 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.