制作一个简单易用的动态MySQL类,可以读取和写入数据库

时间:2016-12-20 18:29:57

标签: php mysql

我想创建一个脚本,通过PHP运行任何查询,就像通过调用MySQL::query([the query]);一样通过MySQL控制台运行它。

我已经创建了很多PHP脚本,但我认为我已经陷入困境。到目前为止,它可以很好地提取信息,但当我尝试对数据库本身进行任何更改时,它不喜欢它。我也希望它也写入MySQL,但我无法弄清楚如何动态地做到这一点。

这是我到目前为止的代码:

<?php

    include '..\..\shared\Error_Code.php';

    class MySQL {
        private static $host;
        private static $username;
        private static $password;
        private static $database;
        private static $logged_in = false;

        public static function login($host, $username, $password, $database) {
            self::$host = $host;
            self::$username = $username;
            self::$password = $password;
            self::$database = $database;
            if (self::confirm_login()) {
                self::$logged_in = true;
            } else {
                self::$logged_in = false;
                Error_Code::print(107.0); //There was an issue with the login.
            }
        }
        public static function change_database($database) {
            if (self::$logged_in) {
                $old_database = self::$database;
                self::$database = $database;
                if (!self::confirm_login()) {
                    self::$database = $database;
                    Error_Code::print(107.1); //The database you entered dose not exist.
                }
            } else {
                Error_Code::print(107.2); //You have not logged in yet.
            }
        }
        private static function confirm_login($host = -1, $username = -1, $password = -1, $database = -1) {
            $host = (($host != -1) ? $host : self::$host);
            $username = (($username != -1) ? $username : self::$username);
            $password = (($password != -1) ? $password : self::$password);
            $database = (($database != -1) ? $database : self::$database);

            if (!(is_null(self::$host) || is_null(self::$username) || is_null(self::$password) || is_null(self::$database))) {
                $connection = mysqli_connect($host, $username, $password);

                if ($connection) {
                    $response = mysqli_query($connection, "SHOW DATABASES");

                    $bg_db = array("information_schema", "mysql", "performance_schema", "phpmyadmin", "test");
                    if (!in_array(self::$database, $bg_db)) {
                        while ($row = mysqli_fetch_assoc($response)) {
                            if($row['Database'] == self::$database) {
                                return true;
                            }
                        }
                    }

                    Error_Code::print(107.8); //Login information is wrong.
                    return false;
                } else {
                    Error_Code::print(107.9); //Login information is wrong.
                    return false;
                }
            } else {
                Error_Code::print(107.7); //Login information is missing.
                return false;
            }
        }
        public static function logged_in() {
            return self::$logged_in;
        }
        public static function query($query, $host = -1, $username = -1, $password = -1, $database = -1) {
            $host = (($host != -1) ? $host : self::$host);
            $username = (($username != -1) ? $username : self::$username);
            $password = (($password != -1) ? $password : self::$password);
            $database = (($database != -1) ? $database : self::$database);
            $continue = self::confirm_login($host, $username, $password, $database);

            if ($continue) {
                return new Data($query, $host, $username, $password, $database);
            } else {
                return new Data($query, $host, $username, $password, $database);
                Error_Code::print(107.3); //You have not logged into a database.
            }
        }
    }

    class Data {
        private $query;
        private $host;
        private $username;
        private $password;
        private $database;

        public function __construct($query, $host, $username, $password, $database) {
            if ($query < 0) {
                return $this;
            } else {
                $this->query = $query;
                $this->host = $host;
                $this->username = $username;
                $this->password = $password;
                $this->database = $database;
                return $this->get();
            }
        }

        //get data from a database using an SQL query
        public function get() {
            //query database
            $connection = mysqli_connect($this->host, $this->username, $this->password, $this->database);
            if ($connection) {
                $response = mysqli_query($connection, $this->query);

                //get data from query
                if ($response) {
                    $rows = array();
                    while ($row = mysqli_fetch_array($response, MYSQLI_ASSOC)) {
                        array_push($rows, $row);
                    }
                    return $rows;
                } else {
                    if (true) { 
                        //<----- I THINK THIS IS WHERE I WOULD HAVE TO ADD THE CODE FOR MAKING CHANGES TO THE DATABASE 
                        //$prepare = mysqli_prepare($connection, $this->query);
                        //mysqli_stmt_execute($prepare);
                    } else {
                        Error_Code::print(108.0); //Could not run query because there was no response
                        return -1;
                    }
                }
            } else {
                Error_Code::print(108.1); //Could not run query because there was no connection
                return -1;
            }
        }

        //print the results of the query to a table
        public function print_table($table_id = -1) {
            $data = $this->get();
            if ($data < 0) {
                Error_Code::print(108.2); //Could not build table do to an issue with the query.
                return -1;
            } else {
                $th = "<tr>";
                foreach ($data[0] as $col => $value) {
                    $th = "$th<th>$col</th>";
                }
                $th = "$th</tr>";

                $td = "";
                foreach ($data as $row) {
                    $td = "$td<tr>";
                    foreach ($row as $col => $value) {
                        $td = "$td<td>$value</td>";
                    }
                    $td = "$td</tr>";
                }

                $table_id = ($table_id < 0) ? "" : " id='$table_id'";
                echo "<table class='database_data'$table_id>$th$td</table>";
                return 1;
            }
        }
    }

?>

有关我可以更改的内容的任何想法,以便将其转移到INSERTDELETEUPDATE以及其他所有内容。

1 个答案:

答案 0 :(得分:0)

检查一下: https://github.com/indieteq/indieteq-php-my-sql-pdo-database-class

对于您的需求可能有点过分,但在我看来,这是如何构建数据库类的一个很好的例子