MySQL 和PHP:自动连接到数据库或以正确的方式将主机/数据库传递给 MySQL 方法

发布于 2024-10-10 03:33:44 字数 206 浏览 0 评论 0原文

有谁知道 PHP 中的一种已知方法可以在应用程序在多个主机上使用多个数据库的情况下自动连接到 MySQL 数据库/表?

问题 1:是否有脚本允许根据查询自动连接到必要的主机/数据库?

问题 2:如果上述不可能,是否有已知的方法可以正确传递主机/数据库信息,以确保应用程序在执行查询之前正确连接?

does anyone know of a known method in PHP to auto connect to MySQL db/table in case an app is using multiple databases on multiple hosts?

Question 1: are there scripts around that allow to auto connect to necessary host/DB based on query?

Question 2: if above is not possible, is there a known approach to properly passing host/DB info to make sure app is properly connected before executing the query?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

腻橙味 2024-10-17 03:33:44

您应该编写一个包装类。这个类将有一个函数query。从这里,您有一些选择:

  1. 将登录信息硬编码到函数中。
  2. 在 Apache 配置中设置服务器变量。
  3. 每次将登录信息传递给您的查询函数。

如果您使用选项 1 或 2,您可能希望将查询的组成部分拆分为多个参数,因此您的 query 函数看起来更像 query($operation, $columns, $表名、$whereClause、$orderBy)。您还可以添加其他参数,但这只是如何执行此操作的示例。


包装类的另一种可能性(这是我更喜欢的方法)是创建该类的实例,每个实例都有自己的 MySQL 连接。只需为您将使用的每个连接创建该类的一个实例,在脚本开头连接到它,然后为您的查询使用正确的实例。例如:

<?php
require_once("MYSQL.php");
$db1 = new MYSQL("database1", "username1", "password1", "hostName1");
$db2 = new MYSQL("database2", "username2", "password2", "hostName2");

$index = $db1->query("SELECT * FROM tableOnDB1") or die($db1->error());
while ($result = $db1->fetch($index))
    echo $result["column1"];

$index = $db2->query("SELECT * FROM tableOnDB2") or die($db2->error());
list($value) = $db2->fetch($index, "ROW");
echo $value;
?>

这是 MYSQL.php:

<?php
    class MYSQL_fetch extends MYSQL
    {
        protected static function fetch($index, $flag = "", $mysql = "")
        {
            if (empty($mysql))
                $mysql = $flag;

            if (!$mysql->ivalid($index))
                return false;

            if ($flag == self::ROW)
                $return = mysql_fetch_row($mysql->results[$index][1]);
            else if ($flag == self::ARR)
                $return = mysql_fetch_array($mysql->results[$index][1]);
            else if ($flag == self::OBJ)
                $return = mysql_fetch_object($mysql->results[$index][1]);
            else
                $return = mysql_fetch_assoc($mysql->results[$index][1]);

            if ($return === false)
                mysql_free_result($mysql->results[$index][1]);

            return $return;
        }

        protected static function querystr($index, $mysql)
        {
            return $mysql->results[$index][0];
        }
    }

    class MYSQL
    {
        protected $connection;
        protected $db;
        protected $type;
        protected $results;
        protected $errormsg;
        protected $valid_instance;

        const E_INDEF_METHOD    = "Undefined method: ";
        const E_INVALID         = "Invalid instance of MYSQL";
        const E_BOUNDS          = "Invalid MYSQL index identifier: ";
        const E_FAIL_PROG_MOVE  = "Cannot move progression, result is either invalid or out of bounds";
        const E_FAIL_PROG_SET   = "Cannot set progression, result is either invalid or out of  bounds";

        const ASSOC             = "ASSOC";
        const ROW               = "ROW";
        const ARR               = "ARRAY";
        const OBJ               = "OBJECT";

        function __construct($db, $server = "", $username = "", $password = "")
        {
            if (get_class($this) != "MYSQL")
            {
                $this->valid_instance = false;
                $this->errormsg = self::E_INVALID;
                return;
            }

            $this->valid_instance = true;
            $this->errormsg = false;
            $this->db = $db;
            $this->results = array();
            $this->type = "MYSQL_fetch";

            $this->connection = @mysql_connect($server, $username, $password, true);
            if ($this->connection)
            {
                if (mysql_select_db($db, $this->connection))
                    register_shutdown_function(array(&$this, "close"));
                else
                    $this->errormsg = mysql_error();
            }
            else
                $this->errormsg = mysql_error();
        }

        function __destruct()
        {
            $this->close();
        }

        private function valid()
        {
            return ($this->valid_instance && isset($this->connection));
        }

        protected function ivalid($index)
        {
            if (!$this->valid())
                return false;

            if (is_numeric($index) && $index < count($this->results))
                return true;
            else
                die("error: ". count($this->results));

            $this->errormsg = self::E_BOUNDS . $index;
            return false;
        }

        public function query()
        {
            if (!$this->valid())
                return false;

            $args = func_get_args();
            $sql = trim(array_shift($args));

            if (func_num_args() > 1)
            {
                foreach ($args as $value)
                {
                    $pos = strpos($sql, "?");
                    if ($pos !== false)
                    {
                        if (is_array($value))
                        {
                            $new = "(";
                            foreach ($value as $val)
                                $new .= "'". mysql_real_escape_string($val) ."', ";
                            $sql = substr_replace($sql, trim($new, ", ") .")", $pos, 1);
                        }
                        else
                            $sql = substr_replace($sql, "'". mysql_real_escape_string($value) ."'", $pos, 1);
                    }
                }
            }

            if (!$resource = @mysql_query($sql, $this->connection))
            {
                $backtrace = debug_backtrace();
                if ($this->errormsg == "")
                    $this->errormsg = "<b>SQL Error at Line ". $backtrace[0]["line"] ." in ". $backtrace[0]["file"] .":</b><br />". mysql_error($this->connection) ."<pre>$sql</pre>";
                return false;
            }

            $this->results[] = array($sql, $resource);
            return count($this->results);
        }

        public function close()
        {
            if (isset($this->connection))
            {
                @mysql_close($this->connection);
                unset($this->connection);
            }
        }

        public function __call($method, $args)
        {
            if (!$this->valid())
                return false;

            if (!method_exists($this->type, $method))
            {
                $this->errormsg = self::E_INDEF_METHOD . $method;
                return false;
            }

            --$args[0];
            $args[] = &$this;
            return call_user_func_array($this->type ."::". $method, $args);
        }

        public function error()
        {
            if (is_array($this->errormsg))
                return htmlentities($this->errormsg["message"]);

            return $this->errormsg;
        }

        public function clearError()
        {
            $this->errormsg = "";
        }

        public function affected_rows()
        {
            return mysql_affected_rows($this->connection);
        }

        public function insert_id()
        {
            return mysql_insert_id($this->connection);
        }

        public function escape($value)
        {
            return mysql_real_escape_string($value);
        }
    }
?>

如果您正确使用它,它还会为您清理输入。另一个样本:

$mysql->query("SELECT * FROM table WHERE value = ?", $value) or die($mysql->error());
$mysql->query("SELECT * FROM table WHERE value IN ?", array($value1, $value2, $value3)) or die($mysql->error());
$mysql->query("INSERT INTO table (column1, column2) VALUES ?", array($value1, $value2)) or die($mysql->error());

You should write a wrapper class. This class will have a function, query. From here, you have some options:

  1. Hard-code the login info into the function.
  2. Set server variables in the Apache config.
  3. Pass the login info to your query function every time.

If you use option 1 or 2, you will probably want to split the components of your query into multiple parameters, so your query function would look more like query($operation, $columns, $tableName, $whereClause, $orderBy). There are other parameters you could possibly add as well, but this is just an example of how you could do it.


Another possibility with your wrapper class (which is the method I prefer) would be to create instances of the class, each with its own MySQL connection. Just create one instance of the class for each connection you will use, connect to it at the beginning of the script, and just use the correct instance for your query. For example:

<?php
require_once("MYSQL.php");
$db1 = new MYSQL("database1", "username1", "password1", "hostName1");
$db2 = new MYSQL("database2", "username2", "password2", "hostName2");

$index = $db1->query("SELECT * FROM tableOnDB1") or die($db1->error());
while ($result = $db1->fetch($index))
    echo $result["column1"];

$index = $db2->query("SELECT * FROM tableOnDB2") or die($db2->error());
list($value) = $db2->fetch($index, "ROW");
echo $value;
?>

Here is MYSQL.php:

<?php
    class MYSQL_fetch extends MYSQL
    {
        protected static function fetch($index, $flag = "", $mysql = "")
        {
            if (empty($mysql))
                $mysql = $flag;

            if (!$mysql->ivalid($index))
                return false;

            if ($flag == self::ROW)
                $return = mysql_fetch_row($mysql->results[$index][1]);
            else if ($flag == self::ARR)
                $return = mysql_fetch_array($mysql->results[$index][1]);
            else if ($flag == self::OBJ)
                $return = mysql_fetch_object($mysql->results[$index][1]);
            else
                $return = mysql_fetch_assoc($mysql->results[$index][1]);

            if ($return === false)
                mysql_free_result($mysql->results[$index][1]);

            return $return;
        }

        protected static function querystr($index, $mysql)
        {
            return $mysql->results[$index][0];
        }
    }

    class MYSQL
    {
        protected $connection;
        protected $db;
        protected $type;
        protected $results;
        protected $errormsg;
        protected $valid_instance;

        const E_INDEF_METHOD    = "Undefined method: ";
        const E_INVALID         = "Invalid instance of MYSQL";
        const E_BOUNDS          = "Invalid MYSQL index identifier: ";
        const E_FAIL_PROG_MOVE  = "Cannot move progression, result is either invalid or out of bounds";
        const E_FAIL_PROG_SET   = "Cannot set progression, result is either invalid or out of  bounds";

        const ASSOC             = "ASSOC";
        const ROW               = "ROW";
        const ARR               = "ARRAY";
        const OBJ               = "OBJECT";

        function __construct($db, $server = "", $username = "", $password = "")
        {
            if (get_class($this) != "MYSQL")
            {
                $this->valid_instance = false;
                $this->errormsg = self::E_INVALID;
                return;
            }

            $this->valid_instance = true;
            $this->errormsg = false;
            $this->db = $db;
            $this->results = array();
            $this->type = "MYSQL_fetch";

            $this->connection = @mysql_connect($server, $username, $password, true);
            if ($this->connection)
            {
                if (mysql_select_db($db, $this->connection))
                    register_shutdown_function(array(&$this, "close"));
                else
                    $this->errormsg = mysql_error();
            }
            else
                $this->errormsg = mysql_error();
        }

        function __destruct()
        {
            $this->close();
        }

        private function valid()
        {
            return ($this->valid_instance && isset($this->connection));
        }

        protected function ivalid($index)
        {
            if (!$this->valid())
                return false;

            if (is_numeric($index) && $index < count($this->results))
                return true;
            else
                die("error: ". count($this->results));

            $this->errormsg = self::E_BOUNDS . $index;
            return false;
        }

        public function query()
        {
            if (!$this->valid())
                return false;

            $args = func_get_args();
            $sql = trim(array_shift($args));

            if (func_num_args() > 1)
            {
                foreach ($args as $value)
                {
                    $pos = strpos($sql, "?");
                    if ($pos !== false)
                    {
                        if (is_array($value))
                        {
                            $new = "(";
                            foreach ($value as $val)
                                $new .= "'". mysql_real_escape_string($val) ."', ";
                            $sql = substr_replace($sql, trim($new, ", ") .")", $pos, 1);
                        }
                        else
                            $sql = substr_replace($sql, "'". mysql_real_escape_string($value) ."'", $pos, 1);
                    }
                }
            }

            if (!$resource = @mysql_query($sql, $this->connection))
            {
                $backtrace = debug_backtrace();
                if ($this->errormsg == "")
                    $this->errormsg = "<b>SQL Error at Line ". $backtrace[0]["line"] ." in ". $backtrace[0]["file"] .":</b><br />". mysql_error($this->connection) ."<pre>$sql</pre>";
                return false;
            }

            $this->results[] = array($sql, $resource);
            return count($this->results);
        }

        public function close()
        {
            if (isset($this->connection))
            {
                @mysql_close($this->connection);
                unset($this->connection);
            }
        }

        public function __call($method, $args)
        {
            if (!$this->valid())
                return false;

            if (!method_exists($this->type, $method))
            {
                $this->errormsg = self::E_INDEF_METHOD . $method;
                return false;
            }

            --$args[0];
            $args[] = &$this;
            return call_user_func_array($this->type ."::". $method, $args);
        }

        public function error()
        {
            if (is_array($this->errormsg))
                return htmlentities($this->errormsg["message"]);

            return $this->errormsg;
        }

        public function clearError()
        {
            $this->errormsg = "";
        }

        public function affected_rows()
        {
            return mysql_affected_rows($this->connection);
        }

        public function insert_id()
        {
            return mysql_insert_id($this->connection);
        }

        public function escape($value)
        {
            return mysql_real_escape_string($value);
        }
    }
?>

It also sanitizes input for you if you use it properly. Another sample:

$mysql->query("SELECT * FROM table WHERE value = ?", $value) or die($mysql->error());
$mysql->query("SELECT * FROM table WHERE value IN ?", array($value1, $value2, $value3)) or die($mysql->error());
$mysql->query("INSERT INTO table (column1, column2) VALUES ?", array($value1, $value2)) or die($mysql->error());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文