与PHP类的数据库连接(mysqli)

时间:2019-03-10 20:17:18

标签: php ajax class mysqli connection

我正在尝试通过php类改善数据库连接。我在这里找到了一些不错的解决方案,并将其实现到我的代码中。我需要帮助来登录:

javascript / jquery:

userService = new UserService();

userService.login(user).done(function (data) {

            if (data) {
                console.log('yess');
                window.location.href = "./AdminPanel.html";
            } else {
                console.log('wroooooong')
            }
}

user.service.js

function UserService() {}

UserService.prototype.isAdmin = function(currentUser) {
    var dataService = {};
    dataService['reason'] = "isAdmin";
    dataService['user'] = currentUser;
    return $.ajax({
        type: "POST",
        data: {
            loginData : (currentUser)
        },
        url: "php/controller/UserController.php",
        dataType: "json",
        success: function(data) {
            return true;
        },
        error: function (data) {
            return false;
        }
    });
};

UserService.prototype.login = function(user) {
    var dataService = {};
    dataService['reason'] = "login";
    dataService['user'] = user;
    return $.ajax({
        type: "POST",
        data: {dataService : dataService},
        url: "./php/BackendHandler.php",
        async: false,
        dataType: "json",
        success: function(data) {
            console.log(data);
            return data;
        },
        error: function (data) {
            console.log(data);
            return data;
        }
    });
};

UserService.prototype.loginAdmin = function() {

};

UserService.prototype.register = function(user) {

};

UserService.prototype.destroy = function(user) {

};

我在ajax / jquery中使用了某种回调函数(返回)

BackendHandler.php

<?php

require "./controller/UserController.php";

$dataService = $_POST['dataService'];

switch ($dataService['reason']) {
    case "login":

        $login = new UserController();
        $login->login($dataService['user']);

        break;
    default:
        echo false;
        break;
}

UserController.php

<?php

$path = "./services/UserService.php";

require "$path";

require('./Connect.php');

class UserController
{
    private $connection;

    private $username;
    private $password;

    function __construct()
    {
        $this->connection = new Connect();
    }

    public function login($userdata)
    {
        $this->username = $userdata['username'];
        $this->password = $userdata['password'];
        $this->password = md5(mysqli_real_escape_string($this->connection->connect(), $userdata['password']));

        $abfrage = "SELECT username, password FROM user WHERE username='$this->username' AND password='$this->password' LIMIT 1";
        $getLogin = mysqli_query($this->connection->connect(), $abfrage);
        $row = mysqli_num_rows($getLogin);


        while ($row == 1) {
            $firma = $row["companyID"];
        }

        echo $firma;

        $this->connection->disconnect();
    }

    private function register()
    {

        $this->connection->disconnect();
    }

    private function destroy()
    {

        $this->connection->disconnect();
    }

    private function getAllUsers()
    {    
        $this->connection->disconnect();
    }

}

Connect.php

include "DbConnection.php";

class Connect
{
    protected $connectionToDatabase = false;
    protected $charset = "utf8";

    function __construct() {
        $this -> connectionToDatabase = new DbConnection();
    }

    public function connect(){
        return $this->connectionToDatabase ->dbConnect();
    }

    public function disconnect(){
        return $this->connectionToDatabase -> dbDisconnect();
    }
}

DbConnection.php

<?php

include_once('Dbconfig.php');

class DbConnection extends Dbconfig {

    public $connectionLink;
    public $dataSet;
    private $sqlQuery;

    protected $databaseName;
    protected $hostName;
    protected $userName;
    protected $passCode;

    function __construct() {
        $this -> connectionLink = NULL;
        $this -> sqlQuery = NULL;
        $this -> dataSet = NULL;

        $dbPara = new Dbconfig();

        $this -> databaseName = $dbPara -> dbName;
        $this -> hostName = $dbPara -> serverName;
        $this -> userName = $dbPara -> userName;
        $this -> passCode = $dbPara ->passCode;

        // $this->dbConnect();
    }

    function dbConnect() {
        $this -> connectionLink = mysqli_connect($this -> hostName, $this -> userName,$this -> passCode);
        mysqli_select_db( $this -> connectionLink, $this -> databaseName);
        return $this -> connectionLink;
    }

    function dbDisconnect() {
        $this -> connectionLink = NULL;
        $this -> sqlQuery = NULL;
        $this -> dataSet = NULL;
        $this -> databaseName = NULL;
        $this -> hostName = NULL;
        $this -> userName = NULL;
        $this -> passCode = NULL;
    }
}

Dbconfig.php

<?php

class Dbconfig {
    protected $serverName;
    protected $userName;
    protected $passCode;
    protected $dbName;

    function __construct() {
        $this -> serverName = 'myServer';
        $this -> userName = 'stack';
        $this -> passCode = 'overflow';
        $this -> dbName = 'myDB';
    }
}

您能否完成缺少的或不太好的编程零件。我对所有分解都有些困惑。

0 个答案:

没有答案