drmeu26880 2011-06-17 18:55
浏览 299
已采纳

为什么mysqli_connect不工作但mysql_connect是?

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). I would like to use mysqli as I hear it's much better/faster. Right now I'm getting error code 2003.

  • 写回答

3条回答 默认 最新

  • dpwuvfpu52502 2011-06-17 19:01
    关注

    mysql_connect() and mysqli_connect() use two different default ports from the php.ini file. 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.

    mysqli_connect:

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

    mysql_connect:

    If the PHP directive mysql.default_host is undefined (default), then the default value is 'localhost:3306'. In SQL safe mode, this parameter is ignored and value 'localhost:3306' is always used.

    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(). Below is the example from 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. 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.

    Source

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿