我已经有了自己的DB.php,在我将MySQL端口更改为3306以外的其他默认功能之前,它是可以工作的。它现在无法连接;我在端口更改后配置了DB.php,如下所示:
class DB {
// server
private $host = "192.168.1.15";
private $uname = "user";
private $pw = "iamuser";
private $dbname = "matlab";
private $con = null;
private $port = "669";
public function __construct() {
$this->con = mysql_connect($this->host,$this->port, $this->uname, $this->pw);
if (!$this->con) {
$this->db_error("Cannot connect to DBMS");
}
mysql_query("SET NAMES 'latin5'", $this->con);
mysql_query("SET CHARACTER SET 'latin5'", $this->con);
mysql_query("SET COLLATION_CONNECTION = 'latin5_turkish_ci'", $this->con);
mysql_select_db($this->dbname, $this->con) or db_error("Can not connect to Database");
}有人能告诉我怎样才能再使用669端口吗?谢谢。
发布于 2014-11-20 09:14:13
解决办法:
您将错误的参数传递给mysql_connect函数,port不是单独的参数。第一个参数接受值为[hostname:port]。所以应该是:
mysql_connect($this->host.':'.$this->port, $this->uname, $this->pw);参考资料:
http://php.net/manual/en/function.mysql-connect.php
附注:
注意:不推荐使用MySQL库,请改用http://php.net/manual/en/book.mysqli.php或http://php.net/manual/en/ref.pdo-mysql.php
发布于 2020-10-04 15:15:25
这是PDO的方法。
class Db extends \PDO {
public function __construct() {
$aDriverOptions[\PDO::MYSQL_ATTR_INIT_COMMAND] = 'SET NAMES UTF8';
parent::__construct('mysql:host=' . Config::DB_HOST . ':80;port=3307;dbname=' . Config::DB_NAME . ';', Config::DB_USR, Config::DB_PWD, $aDriverOptions);
$this->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
}
}最重要的部分是构造函数'mysql:host=xxx;port=xxx;dbname=xxx'的第一个参数,其中必须指定其他3306端口号。在我的例子中,我使用了3307。
https://stackoverflow.com/questions/27035670
复制相似问题