简体   繁体   English

为什么mysqli_connect不工作但mysql_connect是?

[英]Why isn't mysqli_connect working but mysql_connect is?

Are there any reasons why this might be the case? 有什么理由可能出现这种情况吗?

Here's the code: 这是代码:

$dbc = mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
echo $dbc;
if(!$dbc){
die('could not open a connection'.mysqli_connect_errno() . mysqli_connect_error());
}

Again, if I replace mysqli with mysql I get a returned dbc resource id (I assume that's good). 再次,如果我用mysql替换mysqli,我得到一个返回的dbc资源id(我认为这很好)。 I would like to use mysqli as I hear it's much better/faster. 我想使用mysqli,因为我听到它更好/更快。 Right now I'm getting error code 2003. 现在我收到错误代码2003。

mysql_connect() and mysqli_connect() use two different default ports from the php.ini file. mysql_connect()mysqli_connect()使用php.ini文件中的两个不同的默认端口。 I wouldn't guess that these would be different than the standard default 3306, but worth a check or try adding the path to the host url. 我不认为这些将与标准默认3306不同,但值得检查或尝试添加到主机URL的路径。

mysqli_connect : mysqli_connect

mysqli mysqli_connect ( ... int $port = ini_get(" mysqli.default_port ") ... ) mysqli mysqli_connect(... int $ port = ini_get(“ mysqli.default_port ”)...)

mysql_connect : mysql_connect

If the PHP directive mysql.default_host is undefined (default), then the default value is 'localhost:3306'. 如果PHP指令mysql.default_host未定义(默认),则默认值为“localhost:3306”。 In SQL safe mode, this parameter is ignored and value 'localhost:3306' is always used. 在SQL安全模式下,将忽略此参数,并始终使用值“localhost:3306”。

Edit: I was wrong because I didn't read the manual page far enough. 编辑:我错了,因为我没有阅读手册页足够远。

I think your connection is fine, but you wouldn't want to check the object it returns as you have above. 我认为您的连接很好,但您不希望检查它返回的对象,如上所述。 You'd want to use mysqli_connect_errno() . 你想使用 mysqli_connect_errno() Below is the example from PHP.net. 以下是PHP.net的示例。

 
 
 
  
  $mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db'); if (mysqli_connect_error()) { die('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error()); }
 
  

Note: 注意:
OO syntax only: If a connection fails an object is still returned. 仅限OO语法:如果连接失败,仍会返回对象。 To check if the connection failed then use either the mysqli_connect_error() function or the mysqli->connect_error property as in the preceding examples. 要检查连接是否失败,请使用mysqli_connect_error()函数或mysqli-> connect_error属性,如前面的示例所示。

Source 资源

mysqli_connectmysql_connect采用不同的参数并返回不同的值。

考虑使用

$dbc= new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM