MySQL随机分配第二个端口/PHP


MySQL randomly assigns second port / PHP

MyMySQL随机为请求的地址分配第二个端口,如下所示:

[2002]无法分配请求的地址(尝试通过连接tcp://127.0.0.1:3306:3306)

这种行为是在我的本地主机和服务器上触发的,所以我认为这可能是我的代码。我通过一个自己编写的类进行连接,该类只使用常量进行连接(这是正确的,在这些常量中没有分配第二个端口),所以我不知道为什么有时会触发这种行为,也不知道第二个港口来自哪里。出现此错误时,脚本的执行将终止。

如果有人想浏览的话,我在这篇文章中添加了这个类。

欢迎任何建议来解决或绕过此问题。

提前感谢!

    class mysql{
        protected $query                = false;
        protected $lastquery            = false;        
        protected $result               = false;
        protected $row                  = false;
        protected $args                 = array('');
        protected static $mysqli        = null;
        public function __construct('mysqli $mysqli = null){
            self::$mysqli = $mysqli;
            $this->establishAutoConnection();
        }
        // Tries to establish connection, if none is set.
        protected function establishAutoConnection(){
            IF(empty(self::$mysqli)):
                SWITCH($_SERVER['SERVER_NAME']):
                    case 'localhost':
                        self::$mysqli = new mysqli(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWD, MYSQL_DATABASE);
                        break;
                    case 'AN IP.IP.IP':
                        $this->connectToSlave();
                        break;
                    default:
                        self::$mysqli = new mysqli(MYSQL_HOST_SERVER, MYSQL_USER_SERVER, MYSQL_PASSWD_SERVER, MYSQL_DATABASE_SERVER);
                ENDSWITCH;
            ENDIF;
        }
        public function connectToSlave(){
            self::$mysqli = new mysqli(SLAVE_HOST_SERVER, SLAVE_USER_SERVER, SLAVE_PASSWD_SERVER, SLAVE_DATABASE_SERVER);
        }
        public function connectToMaster(){
            self::$mysqli = new mysqli(MASTER_HOST_SERVER, MASTER_USER_SERVER, MASTER_PASSWD_SERVER, MASTER_DATABASE_SERVER);
        }
        // Sets the PDO arguments, which need to be replaced by '?'
        public function setArgs(&$data, $type = false){
            $type = $type ?: $this->getTypeString($data);
            $this->args[0] .= $type;
            $this->args[] = &$data;
            return $this; 
        }
        // Reset function needs to be called in order to make a new query.
        public function reset(){
            $this->args     = array('');
            $this->row      = false;
            $this->result = false;
            return $this;
        }
        // Loops through the found results.
        public function loopThroughResults(){
            return ($this->row = $this->result->fetch_assoc())
                ? true
                : false;
        }
        // Returns the row unformatted. If no result is found an emtpy array is returned.
        public function getRow(){
            $this->row = $this->row ?: $this->result->fetch_assoc();
            return $this->row ?: array();
        }
        // Returns the first result of the first row.
        public function getSingleResult(){
            FOREACH($this->getRow() as $assoc => $value):
                return $value ?: false;
            ENDFOREACH;
            return false;
        }
        // Returns the result by rowname
        public function getByName($name){
            return isset($this->row[$name]) 
                ? $this->row[$name]
                : false;
        }
        // If a new query is made, while the former query has not been resetted, a warning is stored or an error is thrown.
        protected function isResetted(){
            IF($this->result):
                $this->warning("PDO has not been resetted from query: ". $this->lastquery ." // new query: ". $this->query);
            ENDIF;
        }
        // Executes the prepared query.
        public function query($sql){
            $this->query = $sql;
            $this->isResetted();
            $this->lastquery = $sql;
            IF($prep = self::$mysqli->prepare($this->query)):
                IF(count($this->args) > 1):
                    call_user_func_array(array($prep, 'bind_param'), $this->args);
                ENDIF;
                $prep->execute();
                $this->result = $prep->get_result();
                $prep->close();
            ELSE:
                $this->error("Query $sql failed to prepare.");
            ENDIF;
        }
        // Automatically generates the string of types for the submitted params if not set. ("ssisdss") etc.
        protected function getTypeString($string){
            SWITCH(gettype($string)):
                case 'string':
                    return 's';
                case 'double':
                    return 'd';
                case 'boolean':
                case 'integer':
                    return 'i';
                case 'array':
                    $this->error('Unserialized array submitted to PDO.');
                    break;
                default:
                    $this->error('Unknown param type submitted to PDO. ' . print_r($string) . ' type: ' . gettype($string));
                    break;
            ENDSWITCH;
        }
        protected function error($msg){
            IF(!new error($msg)):
                trigger_error($msg);
            ENDIF;
        }
        protected function warning($msg){
            IF(!new warning($msg)):
                trigger_error($msg);
            ENDIF;
        }
    }

您在定义中添加了端口号吗?因此,例如

MYSQL_HOST = 127.0.0.1:3306
MYSQL_HOST_SERVER = 127.0.0.1:3306   

mysqli将在您的服务器定义中使用他的默认端口设置。因此,如果您在此处添加端口号,结果将与您的请求错误相同:

MYSQL_HOST = 127.0.0.1:3306:3306
MYSQL_HOST_SERVER = 127.0.0.1:3306:3306