【问题标题】:How to fix PhpStorm error about undefined variable [duplicate]如何修复有关未定义变量的 PhpStorm 错误 [重复]
【发布时间】:2022-01-16 14:51:18
【问题描述】:

这是我的索引文件,

<?php
include 'db.php';
$form = read('form');
//echo '<pre>';
//print_r($forms);
?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title></title>
</head>
<body>
<form action="store.php" method="post">
    <label>
        Username:
        <input type="text" placeholder="username" name="username">
    </label>
    <label>
        Password:
        <input type="text" placeholder="password" name="password">
    </label>
    <label>
        Email:
        <input type="text" placeholder="email" name="email">
    </label>
    <button type="submit" name="submit">
        submit
    </button>
</form>
<table>
    <?php foreach ($form as $user): ?>
        <tr>
            <?php foreach ($user as $item) {
                echo '<td>' . $item . '</td>';
            } ?>
        </tr>
    <?php endforeach; ?>
</table>
</body>

这是我读取数据库的数据库连接文件:

<?php
$servername = "localhost";
$username = "root";
$password = "mhimlaA#1";

try {
    $conn = new PDO("mysql:host=$servername;dbname=mydb", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//    echo "Connected successfully";
} catch (PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
}
function read($where = '')
{
    global $conn;
    $sql = "SELECT * FROM `form` $where LIMIT 1000;";
    $stm = $conn->prepare($sql);
    $stm->execute();
    return $stm->fetchAll(PDO::FETCH_ASSOC);
}

我在这里遇到奇怪的问题,PhpStorm 在商店文件中的$conn 上显示错误。我正在使用此文件将输入文本插入数据库:

<?php
print_r($_POST);
unset($_POST['submit']);
include 'db.php';
$form = read('form');
$sql = "INSERT INTO mydb.form(username, password, email) VALUES (:username,:password,:email)";
$stm = $conn->prepare($sql);
$stm->execute($_POST) or die($conn->errorInfo());
header('location: index.php');

【问题讨论】:

标签: php phpstorm


【解决方案1】:

PHPStorm 是正确的。如果连接到数据库时出现问题,$conn 可以是未定义的。实际问题是您糟糕的错误处理。如果您不知道如何处理它们,请永远不要 try-catch 异常。你在追随货物崇拜。

如果您确实想防止错误日志中的凭据泄漏,则需要删除 try-catch 或在 catch 块中抛出异常。

try {
    $conn = new PDO("mysql:host=$servername;dbname=mydb", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//    echo "Connected successfully";
} catch (PDOException $e) {
    throw new \PDOException($e->getMessage(), (int) $e->getCode());
}

您还应该删除无用的代码,例如or die($conn-&gt;errorInfo())

【讨论】:

  • 这还不是全部。默认情况下,PhpStorm 不检查包含/需要文件中定义的变量(仅检查当前文件)。要识别$conn,OP 需要启用Undefined variable 检查的Search for variable's definition outside the current file 选项。更多信息:youtrack.jetbrains.com/issue/…
  • 我删除了你提到的try catch和or die($conn->errorInfo()),但是conn steel在store.php the(image file)中有未定义的错误。跨度>
猜你喜欢
  • 2013-12-21
  • 1970-01-01
  • 2011-01-07
  • 1970-01-01
  • 1970-01-01
  • 2014-10-09
  • 1970-01-01
  • 2014-11-28
相关资源
最近更新 更多