Advertisement
Hadi1989

Untitled

Jul 12th, 2019
618
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.70 KB | None | 0 0
  1. <?php
  2.     require 'config.php';
  3.    
  4.     class db_class{
  5.         public $host = db_host;
  6.         public $user = db_user;
  7.         public $pass = db_pass;
  8.         public $dbname = db_name;
  9.         public $conn;
  10.         public $error;
  11.        
  12.         public function __construct(){
  13.             $this->connect();
  14.         }
  15.        
  16.         private function connect(){
  17.             $this->conn = new mysqli($this->host, $this->user, $this->pass, $this->dbname);
  18.             if(!$this->conn){
  19.                 $this->error = "Fatal Error: Can't connect to database".$this->conn->connect_error;
  20.                 return false;
  21.             }
  22.         }
  23.        
  24.         public function save($username, $password, $firstname, $lastname){
  25.             $stmt = $this->conn->prepare("INSERT INTO `user` (username, password, firstname, lastname) VALUES(?, ?, ?, ?)") or die($this->conn->error);
  26.             $stmt->bind_param("ssss", $username, $password, $firstname, $lastname);
  27.             if($stmt->execute()){
  28.                 $stmt->close();
  29.                 $this->conn->
  30.                 close();
  31.                 return true;
  32.             }
  33.         }
  34.        
  35.         public function login($username, $password){
  36.             $stmt = $this->conn->prepare("SELECT * FROM `user` WHERE `username` = '$username' && `password` = '$password'") or die($this->conn->error);
  37.             if($stmt->execute()){
  38.                 $result = $stmt->get_result();
  39.                 $valid = $result->num_rows;
  40.                 $fetch = $result->fetch_array();
  41.                 return array(
  42.                     'user_id'=> $fetch['user_id'],
  43.                     'count'=>$valid
  44.                 );
  45.             }
  46.         }
  47.        
  48.         public function user_account($user_id){
  49.             $stmt = $this->conn->prepare("SELECT * FROM `user` WHERE `user_id` = '$user_id'") or die($this->conn->error);
  50.             if($stmt->execute()){
  51.                 $result = $stmt->get_result();
  52.                 $fetch = $result->fetch_array();
  53.                 return array(
  54.                     'firstname'=> $fetch['firstname'],
  55.                     'lastname'=>$fetch['lastname']
  56.                 );
  57.             }  
  58.         }
  59.     }  
  60. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement