Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- #Login
- if(isset($_POST['doLogin'])) :
- $username = mysql_real_escape_string($_POST['username']);
- $password = mysql_real_escape_string($_POST['password']);
- // Getting the details
- $grabDetails = "SELECT `username`, `password`, `id`, `isActive` FROM `users` WHERE username = '".$username."' AND password = '".$password."'";
- $result = mysql_query($grabDetails) or die(mysql_error());
- $echoResult = mysql_fetch_object($result);
- $countResult = mysql_num_rows($result);
- // If found..
- if($countResult == 1 && $echoResult -> isActive == 1) :
- $_SESSION['isLogged'] = true;
- $_SESSION['userId'] = $echoResult -> id;
- $_SESSION['username'] = $echoResult -> username;
- $_SESSION['timestamp'] = time();
- header("Location: /temp/");
- // If user isn't active
- elseif($countResult == 1 && $echoResult -> isActive != 1):
- header("Location: /temp/index.php?code=1");
- // Default error message
- else:
- header("Location: /temp/index.php?code=2");
- endif;
- # DB class til sitet
- <?php
- class Database{
- private $_dbc;
- private $queryError = 'There was an error!';
- //* Database connection
- function setRoot ($dbh, $dbu, $dbp, $dbn) {
- $this -> _dbc = mysql_connect($dbh, $dbu, $dbp) or die(mysql_error());
- mysql_select_db($dbn , $this -> _dbc) or die(mysql_error());
- }
- //* Close connection to 'databaseConnection'.
- function closeDB() {
- mysql_close($this->_dbc);
- }
- //* Sanitize input
- function sanitize ($thisInput) {
- return mysql_escape_string(htmlentities(stripslashes($thisInput)));
- }
- //* Build a query.
- function buildQuery($table, $type, $fields, $groupKey, $groupResponse, $orderKey, $orderType, $limit) {
- $grouped = '';
- //* If $groupKey is set then
- if($groupKey != ''):
- $grouped = " WHERE `".$groupKey."` = '".$groupResponse."'";
- endif;
- //* If $limit is set then
- $limit = '';
- if($limit != '') :
- $limit = " LIMIT ".$limit;
- endif;
- //* If orderKey is set then:
- $order = '';
- if($orderKey != '') :
- $order = " ORDER BY `".$orderKey ."` ". $orderType."";
- endif;
- //* Manage the $type.
- switch($type) :
- case 'SELECT':
- $type = "SELECT ".$fields." FROM `".$table."`";
- break;
- case 'DELETE':
- $type = "DELETE FROM `".$table."`";
- break;
- endswitch;
- //* The return query.
- $returnQuery = "".$type . $grouped . $order . $limit."";
- return $returnQuery;
- }
- //* Get all rows from a table with certain criteria.
- function getRows ($table, $groupKey = '', $groupResponse = '', $orderKey = '', $orderType = '', $limit = '', $fields = '*') {
- $buildQuery = $this->buildQuery($table, 'SELECT', $fields, $groupKey, $groupResponse, $orderKey, $orderType, $limit);
- $runQuery = mysql_query($buildQuery) or die($this->queryError . mysql_error());
- while($rows = mysql_fetch_object($runQuery)):
- $returnObjects[] = $rows;
- endwhile;
- return $returnObjects;
- }
- //* Get rows from a given ID
- function getRowFromId ($table, $groupKey, $groupResponse, $fields = '*') {
- $thisItem = $this->buildQuery($table, 'SELECT', $fields, $groupKey, $groupResponse);
- $returnItem = mysql_query($thisItem) or die($this->queryError . mysql_error());
- return mysql_fetch_object($returnItem);
- }
- //* Count rows from one table.
- function countRows($table, $type, $fields, $groupKey, $groupResponse) {
- $buildQuery = $this->buildQuery($table, $type, $fields, $groupKey, $groupResponse, '','','','');
- $thisReturn = mysql_query($buildQuery);
- return mysql_num_rows($thisReturn);
- }
- //* Figure out the affected rows.
- function affectedRows($query) {
- return mysql_affected_rows();
- }
- //* Insert from an array.
- function insertArray ($table , $insertVals) {
- foreach($insertVals as $key => $val) :
- $keys[] = $key;
- $keyVal[] = '\''.$val.'\'';
- endforeach;
- $keys = implode(', ' , $keys);
- $keyVal = implode(', ', $keyVal);
- $insertQuery = mysql_query("INSERT INTO `".$table."` (".$keys.") VALUES (".$keyVal.")") or die($this->queryError . mysql_error());
- echo 'Added rows: ' . $this->affectedRows($insertQuery);
- }
- //* Update from an array.
- function updateArray ($table , $groupKey, $groupResponse, $updateVals) {
- foreach($updateVals as $key => $val) :
- $items[] = $key.'=\''.$val.'\'';
- endforeach;
- $items = implode(', ' , $items);
- $updateQuery = mysql_query("UPDATE `".$table."` SET ".$items." WHERE `".$groupKey."` = '".$groupResponse."'") or die($this->queryError . mysql_error());
- echo $this->affectedRows($updateQuery);
- }
- // * Delete from an array.
- function deleteArray($table, $groupKey, $deleteVals){
- foreach($deleteVals as $key) :
- mysql_query("DELETE FROM `".$table."` WHERE `".$groupKey."` = '".$key."'") or die($this->queryError . mysql_error());
- endforeach;
- }
- }
Add Comment
Please, Sign In to add comment