删除页面中显示的mysqli_connect警告

时间:2013-06-20 08:22:39

标签: php

$mysqlServer = "***";
$mysqlDb = "***";
$mysqlUser = "***";
$mysqlPass = "***";

$conn = mysqli_connect($mysqlServer, $mysqlUser, $mysqlPass) or die("failed to connect to db");
mysqli_select_db($conn, $mysqlDb) or die("failed to connect select db");

我有这个代码,它的工作没有任何问题。但是,如果我尝试输入错误的SQL服务器或测试它来执行错误。这将显示:

Warning: mysqli_connect(): (HY000/2002): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
failed to connect select db

如果连接sql server时出现问题,我不希望显示警告。我只想要显示自己的错误。

3 个答案:

答案 0 :(得分:9)

2种可能的选择:

答案 1 :(得分:0)

在每个函数隐藏错误之前放置@符号

$conn = @mysqli_connect($mysqlServer, $mysqlUser, $mysqlPass) or die("failed to connect to db");

答案 2 :(得分:0)

试试这个:

$conn = mysqli_connect($mysqlServer, $mysqlUser, $mysqlPass, $mysqlDb);

使用connect作为第四个参数传递数据库名称。