Jump to content

Recommended Posts

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?
}

Link to comment
https://forums.phpfreaks.com/topic/226372-small-login-system/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/226372-small-login-system/#findComment-1168420
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/226372-small-login-system/#findComment-1168466
Share on other sites

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

}

?>

Link to comment
https://forums.phpfreaks.com/topic/226372-small-login-system/#findComment-1168832
Share on other sites

With comments:

<?php

class Mysql
{
  
  public function __construct()
  {
    //this doesn't go anywhere.  Look into PHP scope.  
    $mysqli = new mysqli( 'localhost', 'root', '', 'login' );
  
  }

}

class Query extends Mysql
{
  
  public function loginQuery()
  {
    //again, this does nothing and goes nowhere.
    $query = "SELECT gebruikerid FROM gebruikers WHERE gebruikersnaam = 'wouter' AND wachtwoord = 'test'";
  
  }

}

class User
{
  
  public function __construct()
  {
    
    $this->mysql = new Mysql();
  
  }
  
  public function login()
  {
    //this function doesn't exist anywhere on any class, neither does $query
    $this->mysql->query( $query );
  
  }

}

?>

-Dan

Link to comment
https://forums.phpfreaks.com/topic/226372-small-login-system/#findComment-1168885
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/226372-small-login-system/#findComment-1168901
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/226372-small-login-system/#findComment-1168982
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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