Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
152 views
in Technique[技术] by (71.8m points)

Trying to Convert Sqlite3 to MySQL i am using this in my website PHP

This is a PHP it is based on sqlite3. I am trying to change its database type to MySQL with a database connection using details shared below. But cant do it after several tries. My SQL Database settings are shared in the code as being a starter in coding.

Details of my MySQL Database are

servername = "localhost"  
username = "root"  
DBName = "Users"  
password = ""

i am a new user to stacoverflow and its my first post so guys any help would be really appreciated

<?php
class DBManager extends SQLite3 {
    
    function __construct ($dbFilename, $flags = SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE) {
            parent::__construct($dbFilename, $flags);
    }
    
    public function addUser ($username, $password, $gauthSecret, $isAdmin = 0) {

        $passwordHash = hash('sha256',$password);
        
        $sqlQuery = "INSERT INTO USERS (USERNAME ,PASSWORDHASH ,GAUTHSECRET ,ISADMIN) ";
        $sqlQuery .= "VALUES (:username, :passwordhash, :secret, :isadmin);";

        $stmt = $this->prepare($sqlQuery);

        if ($stmt) {
            $stmt->bindValue(':username', $username, varchar(100));
            $stmt->bindValue(':passwordhash', $passwordHash, varchar(500));
            $stmt->bindValue(':secret', $gauthSecret, varchar(100));
            $stmt->bindValue(':isadmin', $isAdmin, varchar(100));

            if ($stmt->execute()) {
                return true;
            }
        }

        return false;
    }
    
    public function deleteUser ($username) {

        $sqlQuery = "DELETE from USERS where USERNAME=:username;";

        $stmt = $this->prepare($sqlQuery);

        if ($stmt) {
            $stmt->bindValue(':username', $username, );

            if ($stmt->execute()) {
                return true;
            }
        }

        return false;
    }
    
    public function deleteAllUsers () {
        
        $sqlQuery = "DELETE from USERS;";

        if(!($ret = $this->exec($sqlQuery))) {
            return false;
        }
        else {
            return true;
        }
    }

    public function getPasswordHash ($username) {

        return $this->getUserData('PASSWORDHASH', $username);
    }

    protected function getUserData ($column, $username) {

        switch ($column) {
            case 'PASSWORDHASH':
            case 'GAUTHSECRET':
            case 'ISADMIN':
            case '*':
                break;

            default:
                return false;
        }

        $sqlQuery = "SELECT $column from USERS where USERNAME=:username;";

        $stmt = $this->prepare($sqlQuery);

        if ($stmt) {
            $stmt->bindValue(':username', $username, varchar());

            $res = $stmt->execute();
            $row = $res->fetchArray(SQLITE3_ASSOC);

            if ($row) {
                if ($column == '*') {
                    return $row;
                }

                return $row["$column"];
            }
        }

        return false;
    }

    public function getGauthSecret ($username) {

        return $this->getUserData('GAUTHSECRET', $username);
    }

    public function getAdminStatus ($username) {
        
        $status = $this->getUserData('ISADMIN', $username);

        if ($status) {
            return (int)$status;
        }

        return false;
    }

    public function getUserList () {

        $sqlQuery = "SELECT USERNAME,ISADMIN from USERS;";

        if(!($ret = $this->query($sqlQuery))) {
            return false;
        }
        else {
            $result = array();
            while ($row = $ret->fetchArray(SQLITE3_ASSOC)) {
                    $result[$row["USERNAME"]] = $row["ISADMIN"];
            }
            return $result;
        }
    } 

    public function getPasswordHashAndGauthSecret ($username) {

        return $this->getUserData('*', $username);
    }

    public function updatePassword ($username, $password) {
 
        $passwordHash = hash('sha256',$password);
       
question from:https://stackoverflow.com/questions/65651254/trying-to-convert-sqlite3-to-mysql-i-am-using-this-in-my-website-php

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...