{
  "id": "php/mysqli-connection-character-set",
  "signature": "Warning: mysqli::set_charset(): Couldn't fetch mysqli in /var/www/app/src/Database/MysqliConnection.php on line 22",
  "signature_zh": "警告：mysqli::set_charset()：无法获取 mysqli，位于 /var/www/app/src/Database/MysqliConnection.php 第 22 行",
  "regex": "Warning: mysqli::set_charset\\(\\): Couldn't fetch mysqli in .* on line \\d+",
  "domain": "php",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "The mysqli::set_charset() method is called on a connection object that is not connected or has failed to connect, often due to incorrect host, credentials, or database server unavailability.",
  "root_cause_type": "generic",
  "root_cause_zh": "mysqli::set_charset() 方法在一个未连接或连接失败的对象上调用，通常是由于主机、凭据错误或数据库服务器不可用。",
  "versions": [
    {
      "version": "PHP 8.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "PHP 8.1",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "PHP 8.2",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "MySQL 8.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "MariaDB 10.6",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "MariaDB 11.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "The extension is loaded; the issue is that the connection object is invalid.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "The server may be fine; the error is in the application configuration.",
      "fail_rate": 0.85,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "The error is about connection establishment, not script execution duration.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Check the connection before setting charset: `if ($mysqli->connect_errno) { die('Connection failed: ' . $mysqli->connect_error); } $mysqli->set_charset('utf8mb4');`",
      "success_rate": 0.9,
      "how": "Check the connection before setting charset: `if ($mysqli->connect_errno) { die('Connection failed: ' . $mysqli->connect_error); } $mysqli->set_charset('utf8mb4');`",
      "condition": "",
      "sources": []
    },
    {
      "action": "Verify MySQL host, port, username, and password in the connection string; test with `mysql -h host -P port -u user -p` from the command line.",
      "success_rate": 0.85,
      "how": "Verify MySQL host, port, username, and password in the connection string; test with `mysql -h host -P port -u user -p` from the command line.",
      "condition": "",
      "sources": []
    },
    {
      "action": "Ensure the MySQL server is running and accessible; check firewall rules and bind-address in my.cnf (e.g., set `bind-address = 0.0.0.0` for remote connections).",
      "success_rate": 0.75,
      "how": "Ensure the MySQL server is running and accessible; check firewall rules and bind-address in my.cnf (e.g., set `bind-address = 0.0.0.0` for remote connections).",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "在设置字符集之前检查连接状态：`if ($mysqli->connect_errno) { die('连接失败：' . $mysqli->connect_error); } $mysqli->set_charset('utf8mb4');`",
    "验证连接字符串中的 MySQL 主机、端口、用户名和密码；使用 `mysql -h host -P port -u user -p` 从命令行测试。",
    "确保 MySQL 服务器正在运行并可访问；检查 my.cnf 中的防火墙规则和 bind-address（例如，为远程连接设置 `bind-address = 0.0.0.0`）。"
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://www.php.net/manual/en/mysqli.set-charset.php",
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.84,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-01-08",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}