7
$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 server 或测试它以执行错误。这将显示:

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 服务器出现问题,我不希望显示警告。我只想显示我自己的错误。

4

3 回答 3

13

2个可能的选项:

于 2013-06-20T08:24:30.753 回答
0

在每个函数前加上@符号隐藏错误

$conn = @mysqli_connect($mysqlServer, $mysqlUser, $mysqlPass) or die("failed to connect to db");
于 2013-06-20T08:25:19.587 回答
0

试试这个:

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

将带有连接的数据库名称作为第四个参数传递。

于 2013-06-20T08:27:36.367 回答