gpt4 book ai didi

如果未启用 mysqli,PHP 不会抛出异常

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:04:47 24 4
gpt4 key购买 nike

我有

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once "configuration.php";
header('Content-Type: application/json');
try
{
$mysqli = new mysqli(MYSQL_SERVER, MYSQL_USERNAME, MYSQL_PASSWORD, MYSQL_DATABASE);
$mysqli->set_charset("utf8");
} catch (Exception $e) {
echo json_encode(
array(
'msg' => $e->getMessage()
)
);
}

如果 mysqli 未启用,则它不会捕获错误:

Fatal error: Uncaught Error: Class 'mysqli' not found in C:\test\db_connect.php:8
Stack trace:
#0 C:\test\getContacts.php(2): require_once()
#1 {main} thrown in C:\test\db_connect.php on line 8

我该怎么做才能捕获错误?

这个我试过了,但是没用:

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once "configuration.php";
header('Content-Type: application/json');
try
{
if(!extension_loaded('mysqli'))
{
throw new Exception('mysqli is not enabled');
}

$mysqli = new mysqli(MYSQL_SERVER, MYSQL_USERNAME, MYSQL_PASSWORD, MYSQL_DATABASE);
$mysqli->set_charset("utf8");
} catch (Exception $e) {
echo json_encode(
array(
'msg' => $e->getMessage()
)
);
}

这个并没有停顿,继续执行脚本。

{"msg":"mysqli is not enabled"}
Notice: Undefined variable: mysqli in C:\test\getContacts.php on line 99

Fatal error: Uncaught Error: Call to a member function query() on null in C:\test\getContacts.php:99 Stack trace: #0 {main} thrown in C:\test\getContacts.php on line 99

最佳答案

奇怪的是它不会被安装,但如果你自己安装,我想它可以被省略。我会检查程序函数是否存在

if(!function_exists('mysqli_connect')) {
throw new Exception('mysqli is not enabled');
}

关于如果未启用 mysqli,PHP 不会抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38838079/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com