在PHP中从sql表中获取数据时出现问题

problem in fetching data from tables of sql in PHP

提问人:M Taha 提问时间:11/25/2022 更新时间:11/25/2022 访问量:46

问:

嗨,我第一次在 SQL 中创建了多个表,我在获取数据时遇到了困难。所以这是我的代码,

<?php

// Use to fetch product data
class Product
{
    public $db = null;

    public function __construct(DBController $db)
    {
        if (!isset($db->con)) return null;
        $this->db = $db;
    }

    // fetch product data using getData Method
    public function getData($table = 'lipstick', $liner = 'liner', $brush = 'brush', $eyeshadow = 'eyeshadow'){
        $result = $this->db->con->query( "SELECT * FROM {$table}");
        $result1 = $this->db->con->query( "SELECT * FROM {$liner}");
        
        
        $result2 = $this->db->con->query("SELECT * FROM {$eyeshadow}");
        $result3 = $this->db->con->query("SELECT * FROM {$brush}");
                                                                    

        $resultArray = array();

        // fetch product data one by one
        while ($item = mysqli_fetch_array($result, MYSQLI_ASSOC)){
            $resultArray[] = $item;
        }

        return $resultArray;
    }

我只从$result获取数据,因为我想从$result 1-3 获取数据。

我尝试像这样为所有结果变量运行循环,

<?php

// Use to fetch product data
class Product
{
    public $db = null;

    public function __construct(DBController $db)
    {
        if (!isset($db->con)) return null;
        $this->db = $db;
    }

    // fetch product data using getData Method
    public function getData($table = 'lipstick', $liner = 'liner', $brush = 'brush', $eyeshadow = 'eyeshadow'){
        $result = $this->db->con->query( "SELECT * FROM {$table}");
        $result1 = $this->db->con->query( "SELECT * FROM {$liner}");
        
        
        $result2 = $this->db->con->query("SELECT * FROM {$eyeshadow}");
        $result3 = $this->db->con->query("SELECT * FROM {$brush}");
                                                                    

        $resultArray = array();

        // fetch product data one by one
        while ($item = mysqli_fetch_array($result, MYSQLI_ASSOC)){
            $resultArray[] = $item;
        }

        return $resultArray;

          // fetch product data one by one
          while ($item1 = mysqli_fetch_array($result1, MYSQLI_ASSOC)){
            $resultArray[] = $item1;
        }

        return $resultArray;
          // fetch product data one by one
          while ($item2 = mysqli_fetch_array($result2, MYSQLI_ASSOC)){
            $resultArray[] = $item2;
        }

        return $resultArray;
          // fetch product data one by one
          while ($item3 = mysqli_fetch_array($result3, MYSQLI_ASSOC)){
            $resultArray[] = $item3;
        }

        return $resultArray;
    }

    
    

但是我在第74行中收到错误::undefined variable item1-3: 我现在该怎么办。

php mysql sql 数据库 mysqli

评论

1赞 Sammitch 11/25/2022
当你发现自己像这样对变量进行编号时,你可能需要一个数组来代替。当你发现自己在一行中编写几个相同的代码块时,你可能需要一个循环。当您发现自己试图处理 4 个相同的查询结果集时,您可能需要一个。当您有 4 个仅按名称区分的相同表时,您可能存在架构设计和/或规范化问题。$result1/2/3while($item = ...) { ... }UNION
0赞 nbk 11/25/2022
数据库是关系数据库,因此您可以在 Qnoe 查询中加入/合并它们
0赞 Vishal Kamlapure 11/25/2022
此外,您没有得到 1,2,3 结果的原因是,在运行循环后,您将返回数组,因此无法访问下一个代码。$result
0赞 Vishal Kamlapure 11/25/2022
问题缺乏对编程的基本理解。
0赞 M Taha 11/26/2022
Vishal Kamlapure ,感谢您的帮助,基本上我正在学习自己,我没有老师,我唯一的老师是 YouTube,所以我几次感到困惑或陷入困境,所以我试图与专家交流。

答:

0赞 Haselnussstrauch 11/25/2022 #1

你的代码中有一些错误,我为你“纠正”了你的代码。

  1. return 语句退出函数意味着 return 下面的代码未执行
  2. 您可以重用变量
  3. 使用多维数组将多个表存储在一个数组中

我知道这不是查询数据库的最佳方式 - 但我希望它对您有所帮助

public function getData($table = 'lipstick', $liner = 'liner', $brush = 'brush', $eyeshadow = 'eyeshadow'){
    $result = $this->db->con->query( "SELECT * FROM {$table}");
    $result1 = $this->db->con->query( "SELECT * FROM {$liner}");
    $result2 = $this->db->con->query("SELECT * FROM {$eyeshadow}");
    $result3 = $this->db->con->query("SELECT * FROM {$brush}");
                                                                    

    $resultArray = array();

    // fetch product data one by one
    $resultArray[$table] = array();
    while ($item = mysqli_fetch_array($result, MYSQLI_ASSOC)){
        array_push($resultArray[$table], $item);
    }

    //return $resultArray;          //no return here, if you return here the code below will not be executed

    // fetch product data one by one
    $resultArray[$liner] = array();
    while ($item = mysqli_fetch_array($result1, MYSQLI_ASSOC)){     
        array_push($resultArray[$liner], $item);                            
    }

    //return $resultArray;          //no return here, if you return here the code below will not be executed
    // fetch product data one by one
    $resultArray[$eyeshadow] = array();
    while ($item = mysqli_fetch_array($result2, MYSQLI_ASSOC)){     
        array_push($resultArray[$eyeshadow], $item);                                
    }

    //return $resultArray;          //no return here, if you return here the code below will not be executed
    // fetch product data one by one
    $resultArray[$brush] = array();
    while ($item = mysqli_fetch_array($result3, MYSQLI_ASSOC)){     //you can reuse $item
        array_push($resultArray[$brush], $item);                                
    }

    return $resultArray;
}