执行以下代码时出现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
分配了值,并且没有数据库连接错误。
答案 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变量未解析,并且不会被分配的值替换,因为您已将其包含在引号中。