我的MySQL随机地将第二个端口分配给所请求的地址,如下所示:
[2002]无法分配请求的地址(尝试通过tcp://127.0.0.1:3306:3306连接)
此行为也在我的localhost和我的服务器上触发,因此我认为它可能是我的代码。我通过自编写的类连接,它只使用常量来连接(这是正确的,没有在这些常量中分配的第二个端口),所以我很无能为什么有时会触发此行为并且第二个端口来自哪里从。发生此错误时,脚本的执行将终止。
如果有人想要浏览,我在这篇文章中添加了这个课程。
欢迎任何建议修复或环游。
提前致谢!
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;
}
}
答案 0 :(得分:1)
您是否在定义中添加了端口号?例如,
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