Hej,
Jeg har lavet en database class og en login class, men jeg kan simpelthen ikke få dem til at arbejde sammen.
Dette er den error jeg får:
Notice: Undefined variable: db in C:\wamp\www\*******\classes\login.php on line 12
Fatal error: Call to a member function fetch_rows() on a non-object in C:\wamp\www\*******\classes\login.php on line 12
Dette er min database class:
<?php
class database
{
private $connectlink; //Database Connection Link
private $username = "root";
private $password = "";
private $database = "*******";
private $hostname = "localhost";
private $resultlink; //Database Result Recordset link
private $rows; //Stores the rows for the resultset
function __construct()
{
$this->connectlink = mysql_connect($this->hostname,$this->username,$this->password);
if(!($this->connectlink))
{
//throw new DatabaseConnectionException("Error Connecting to the Database".mysql_error(),"101");
}
else
{
mysql_select_db($this->database);
}
}
function __destruct()
{
@mysql_close($this->connectlink);
}
public function query($sql)
{
$this->resultlink = mysql_query($sql);
return $this->resultlink;
}
public function fetch_rows($sql)
{
$result = $this->query($sql);
$rows = array();
if($result)
{
while($row = mysql_fetch_array($result))
{
$rows[] = $row;
}
}
else
{
//throw new RetrieveRecordsException("Error Retrieving Records".mysql_error(),"102");
$rows = null;
}
return $rows;
}
}
$db = new database(); //Create database object
?>
Dette er min login class:
<?php
require_once ('database.php');
class login extends database {
private $Username;
private $Password;
public function do_login($Username, $Password)
{
$db->fetch_rows("SELECT * FROM user_Table WHERE Username=" . $Username . " AND Password=" . $Password . "");
if (($db->$rows) != null)
{
(bool)$_SESSION['loggedIn'] = true;
(string)$_SESSION['uID'] = $db->rows['ID'];
(string)$_SESSION['Firstname'] = $db->rows['Firstname'];
(string)$_SESSION['Surname'] = $db->rows['Surname'];
(string)$error = null;
}
else
{
(bool)$_SESSION['loggedIn'] = false;
(string)$error = "Login failed, you typed a wrong username or password!";
}
if ((bool)$_SESSION['loggedIn'] != null)
{
return header(" location:membersite.php ");
}
else
{
return $error;
}
}
public function do_logout()
{
Unset($_SESSION['loggedIn']);
Unset($_SESSION['uID']);
Unset($_SESSION['Firstname']);
Unset($_SESSION['Surname']);
}
}
$login = new login();
?>
Og dette er min loginside:
<?php
require_once('classes/login.php');
(string)$Username = isset($_POST['Username']);
(string)$Password = isset($_POST['Password']);
if (isset($_POST['logIn']))
{
$login->do_login($Username, $Password);
}
?>
Det hele er lidt uslebet da jeg kun har brugt 1 time på det, men jeg kan simpelthen ikke gennemskue hvorfor den ikke kan genkende $db->, når jeg jo har extended min database class med login..
Håber i kan hjælpe
Indlæg senest redigeret d. 25.01.2010 04:53 af Bruger #14181