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 孟德尔随机化结果不一致
  • ¥20 求用stm32f103c6t6在lcd1206上显示Door is open和password:
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法