Jump to content

Small login system


php-beginner

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

Archived

This topic is now archived and is closed to further replies.

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