注意:未定义的变量:PHP中的id

时间:2018-10-09 06:02:33

标签: php sql

执行以下代码时出现undefined variable错误:

if ($amount != NULL) {
    $host = 'localhost';
    $user = 'root';
    $pass = '';
    $db = 'deedok';
    $connection = mysqli_connect($host, $user, $pass, $db);
    echo 'connected_deedok';
    if (!$conn){
        die('Database Connection Lost.');
    }
        $query_deedok = "SELECT * FROM products WHERE amount='$amount'";
            $query_deedok_result = mysqli_query($connection, $query_deedok);
    if ($query_deedok_result != NULL) {
        while($query_deedok_result_rows = mysqli_fetch_assoc($query_deedok_result)) {
            $name = $query_deedok_result_rows['name'];
            $id = $query_deedok_result_rows['id'];
            $description = $query_deedok_result_rows['description'];
        }
    }
    echo $name;
    echo $id;
    echo $description;
}

我已经为$amount分配了值,并且没有数据库连接错误。

1 个答案:

答案 0 :(得分:1)

执行此操作,它将起作用:

if ($amount) {
                $host = 'localhost';
                $user = 'root';
                $pass = '';
                $db = 'deedok';
                $connection = mysqli_connect($host, $user, $pass, $db);
                echo 'connected_deedok';
                if (!$conn){
                    die('Database Connection Lost.');
                }
                    $query_deedok = "SELECT * FROM products WHERE amount='" .  $amount . "'";
                        $query_deedok_result = mysqli_query($connection, $query_deedok);
                if ($query_deedok_result != NULL) {
                    while($query_deedok_result_rows = mysqli_fetch_assoc($query_deedok_result)) {
                        $name = $query_deedok_result_rows['name'];
                        $id = $query_deedok_result_rows['id'];
                        $description = $query_deedok_result_rows['description'];
                    }
                        }
        echo $name;
        echo $id;
        echo $description;
            }

您的$ amount变量未解析,并且不会被分配的值替换,因为您已将其包含在引号中。