1. Det overstående giver ingen mening...
2. Jeg ville nok først lave et objekt med alle database metoderne...
3. Jeg har givet et forslag på hvordan det kunne laves:
config.php
<?php
//MYSQL CONNECTION
define("MYSQL_USERNAME", "DITBRUGERNAVN");
define("MYSQL_PASSWORD", "DITPASSWORD");
define("MYSQL_DATABASE", "DINDATABASE");
define("MYSQL_HOST", "DINHOST");
?>
mysql.class.php:
<?php
class Mysql
{
//CURRENT CONNECTION
private $Connection;
//CURRENT Query
private $Query;
//CURRENT SELECTED ROWS
private $Rows;
//NUMBERS OF ROWS
private $RowNumbers;
//CONSTRUCTOR
public function __construct()
{
//CONNECT TO DATABASE
$this->Connection = mysql_connect(MYSQL_HOST, MYSQL_USERNAME, MYSQL_PASSWORD);
mysql_select_db(MYSQL_DATABASE, $this->Connection);
}
//QUERY STRING
public function Query($Query)
{
$this->Query = mysql_query($Query, $this->Connection);
if($this->Query)
{
return true;
}else{
return false;
}
}
//GET DATA FROM THE DATABASE
public function GetRows(){
$this->Rows = mysql_fetch_array($this->Query);
return $this->Rows;
}
//GET NUMBERS OF ROWS FROM THE DATABASE
public function GetRowNumbers(){
$this->RowNumbers = mysql_num_rows($this->Query);
return $this->RowNumbers;
}
//GUARD AGAINST QUOTES
public static function Slashes($x)
{
if (!get_magic_quotes_gpc()) {
return addslashes($x);
}else{
return $x;
}
}
}
?>
user.class.php:
<?php
class User {
//CONTAIN DATABASE
private $Database;
//CONSTRUCTOR
public function __construct()
{
$this->Database = new Mysql();
}
public function CheckUser($Username, $Password) {
$Query = "SELECT * FROM Users WHERE username = '".Mysql::Slashes($Username)."' AND password = '".Mysql::Slashes($Password)."'";
if($this->Database->Query($Query)) {
//IF ONLY ONE ROW IS FOUND
if ($this->Database->GetRowNumbers() == 1) {
return true;
}else{
return false;
}
}else{
return false;
}
}
}
?>
Sådan kunne du så bruge det:
example.php
<?php
include("config.php");
include("mysql.class.php");
include("user.class.php");
$User = new User();
//FORSLAG 1
$User->CheckUser("Brugernavn","Password"); //RETUNERE FALSE ELLER TRUE
//FORSLAG 2
if($User->CheckUser("Brugernavn","Password")){
echo "Du er logget ind";
//SAET EVT SESSIONS...
}else{
echo "Du er ikke logget ind...";
}
?>
Det overstående er ikke testet og er bare en ide til hvordan det kan laves....