Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何检测MySQL数据库是否正在运行?_Php_Mysql_Mysqli - Fatal编程技术网

Php 如何检测MySQL数据库是否正在运行?

Php 如何检测MySQL数据库是否正在运行?,php,mysql,mysqli,Php,Mysql,Mysqli,我正在使用XAMPP,我想检测MySQL数据库是否正在运行。我的代码如下所示: $this->connection = mysqli_connect( $this->host, $this->name, $this->pass, $this->db ); if ($this->connection->connect_errno) { $this->connection

我正在使用XAMPP,我想检测MySQL数据库是否正在运行。我的代码如下所示:

$this->connection = mysqli_connect(
        $this->host,
        $this->name,
        $this->pass,
        $this->db
);

if ($this->connection->connect_errno)
{
        $this->connection = false;
        return false;
} else { $this->connection->set_charset('utf-8'); }
我收到以下日志:

PHP警告:mysqli_connect():(HY000/2002):连接被拒绝

PHP注意:试图在//中获取非对象的属性,这指的是$This->connection->connect\u errno

PHP致命错误:调用布尔值上的成员函数set_charset()


我怎样才能防止这种情况?如何检查数据库是否可用?

首先,您需要为
mysqli\u connect
调用禁用或抑制警告报告,或者将其嵌入
try/catch
块中

然后,不要检查
connect\u errno
,而是首先验证
connection
是否为真实值。像这样

$this->connection = false;
try {
    $this->connection = mysqli_connect(
            $this->host,
            $this->name,
            $this->pass,
            $this->db
    );
    if (!$this->connection || $this->connection->connect_errno)
    {
        $this->connection = false;
        return false;
    } else { 
        $this->connection->set_charset('utf-8'); 
    }
} catch ($e) { //something bad happened. Probably not recoverable.
    $this->connection = false;
    return false;
}
$this->connection = @mysqli_connect( //<-- warnings suppressed
        $this->host,
        $this->name,
        $this->pass,
        $this->db
);

if (!$this->connection || $this->connection->connect_errno) //lazy evaluation will prevent any issues here
{
        $this->connection = false;
        return false;
} else { $this->connection->set_charset('utf-8'); }

首先,您需要为
mysqli\u connect
调用禁用或抑制警告报告,或者将其嵌入
try/catch
块中

然后,不要检查
connect\u errno
,而是首先验证
connection
是否为真实值。像这样

$this->connection = false;
try {
    $this->connection = mysqli_connect(
            $this->host,
            $this->name,
            $this->pass,
            $this->db
    );
    if (!$this->connection || $this->connection->connect_errno)
    {
        $this->connection = false;
        return false;
    } else { 
        $this->connection->set_charset('utf-8'); 
    }
} catch ($e) { //something bad happened. Probably not recoverable.
    $this->connection = false;
    return false;
}
$this->connection = @mysqli_connect( //<-- warnings suppressed
        $this->host,
        $this->name,
        $this->pass,
        $this->db
);

if (!$this->connection || $this->connection->connect_errno) //lazy evaluation will prevent any issues here
{
        $this->connection = false;
        return false;
} else { $this->connection->set_charset('utf-8'); }
我不是这个答案的作者,它只是另一篇文章的原始版本,这是给定条件下唯一正确的解决方案


首先,您需要为
mysqli\u connect
调用禁用警告报告或抑制它们

然后,不要检查
connect\u errno
,而是首先验证
connection
是否为真实值。像这样

$this->connection = false;
try {
    $this->connection = mysqli_connect(
            $this->host,
            $this->name,
            $this->pass,
            $this->db
    );
    if (!$this->connection || $this->connection->connect_errno)
    {
        $this->connection = false;
        return false;
    } else { 
        $this->connection->set_charset('utf-8'); 
    }
} catch ($e) { //something bad happened. Probably not recoverable.
    $this->connection = false;
    return false;
}
$this->connection = @mysqli_connect( //<-- warnings suppressed
        $this->host,
        $this->name,
        $this->pass,
        $this->db
);

if (!$this->connection || $this->connection->connect_errno) //lazy evaluation will prevent any issues here
{
        $this->connection = false;
        return false;
} else { $this->connection->set_charset('utf-8'); }
$this->connection=@mysqli\u connect(//主机,
$this->name,
$this->pass,
$this->db
);
如果(!$this->connection | |$this->connection->connect\u errno)//延迟求值将防止出现任何问题
{
$this->connection=false;
返回false;
}else{$this->connection->set_字符集('utf-8');}
我不是这个答案的作者,它只是另一篇文章的原始版本,这是给定条件下唯一正确的解决方案


首先,您需要为
mysqli\u connect
调用禁用警告报告或抑制它们

然后,不要检查
connect\u errno
,而是首先验证
connection
是否为真实值。像这样

$this->connection = false;
try {
    $this->connection = mysqli_connect(
            $this->host,
            $this->name,
            $this->pass,
            $this->db
    );
    if (!$this->connection || $this->connection->connect_errno)
    {
        $this->connection = false;
        return false;
    } else { 
        $this->connection->set_charset('utf-8'); 
    }
} catch ($e) { //something bad happened. Probably not recoverable.
    $this->connection = false;
    return false;
}
$this->connection = @mysqli_connect( //<-- warnings suppressed
        $this->host,
        $this->name,
        $this->pass,
        $this->db
);

if (!$this->connection || $this->connection->connect_errno) //lazy evaluation will prevent any issues here
{
        $this->connection = false;
        return false;
} else { $this->connection->set_charset('utf-8'); }
$this->connection=@mysqli\u connect(//主机,
$this->name,
$this->pass,
$this->db
);
如果(!$this->connection | |$this->connection->connect\u errno)//延迟求值将防止出现任何问题
{
$this->connection=false;
返回false;
}else{$this->connection->set_字符集('utf-8');}

永远不要抑制警告,这是一个糟糕的计划。我会将连接放在一个
try/catch
中,以便更恰当地处理错误。啊,我不知道警告实际上是可以正确捕获的。谢谢你的提示。我已经改进了我的答案。这是你的答案,你可以随意编辑(只要你没有破坏它)。如果这导致了一场回滚战,请标记为mod help.Meta post关于这个答案的编辑:(cc@YourCommonSense)永远不要抑制警告,这是一个糟糕的计划。我会将连接放在一个
try/catch
中,以便更恰当地处理错误。啊,我不知道警告实际上是可以正确捕获的。谢谢你的提示。我已经改进了我的答案。这是你的答案,你可以随意编辑(只要你没有破坏它)。如果这导致了一场回滚战,那么就标记为mod help.Meta发布关于这个答案的编辑:(cc@YourCommonSense)在
try/catch
语句中执行连接,以便您可以优雅地处理错误。您可以显示整个类吗?在
try/catch
语句中执行连接,以便您可以优雅地处理错误。您可以显示整个类吗?