首页 > 解决方案 > 从 MySQL 数据库中获取记录的问题

问题描述

由于这个错误,我在从 MySQL 库获取数据时遇到了很大的问题:

警告:mysqli::close(): 无法在第 44 行的 /Applications/AMPPS/www/Maciej/search.php 中获取 mysqli

试图通过删除连接关闭的行来做一些事情,但最终结果只是没有请求数据的空白页。目标是制作与数据库连接的营养计算器。我试图测试代码是否可以得到任何结果,但如上所述它失败了。IDE 也向我发出尖叫,告诉我它无法解析请求中的列。谢谢您的帮助!

在提示中请写出哪一行可能是问题,因为我们之间的沟通会更好。如果您对代码本身有一些评论,那也会有很大的帮助。大家,谢谢你的帮助!

session_start();

require_once 'connect.php';

$conn = @new mysqli($host, $user, $password, $db_name, $db_table);
if ($conn->connect_errno!=0)
{
    echo 'Error:'.$conn->connect_errno;
}
else
{
    $product = $_GET['product'];
    $amount = $_GET['amount'];

    $sql = "SELECT * FROM Food_db, mytable WHERE Product = '$product'";

    if($result = @$conn->query($sql))
    {
        $how_many = $result->num_rows;
        if ($how_many==1 and $amount>0)
        {
            $row = $result->fetch_assoc();
            $_SESSION['Product']= $row['Product'];
            $_SESSION['Proteins']= $amount * $row['Proteins'];
            $_SESSION['Fats']= $amount * $row['Fats'];
            $_SESSION['Carbohydrates']= $amount * $row['Carbohydrates'];
            $_SESSION['Calories']= $amount * $row['Calories'];


            unset($_SESSION['Error']);
            $result->close();
            header('Location:result.php');

        }else{
            $_SESSION['Amount_error'] = 'You have to add amount!';
            $_SESSION['Error:'] = 'Ups record not in base';
            header('Location:index.php');

        }
    }

    $conn->close();
}

标签: phpmysql

解决方案


推荐阅读