提问人:M Taha 提问时间:11/25/2022 更新时间:11/25/2022 访问量:46
在PHP中从sql表中获取数据时出现问题
problem in fetching data from tables of sql in PHP
问:
嗨,我第一次在 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: 我现在该怎么办。
答:
0赞
Haselnussstrauch
11/25/2022
#1
你的代码中有一些错误,我为你“纠正”了你的代码。
- return 语句退出函数意味着 return 下面的代码未执行
- 您可以重用变量
- 使用多维数组将多个表存储在一个数组中
我知道这不是查询数据库的最佳方式 - 但我希望它对您有所帮助
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;
}
评论
$result1/2/3
while($item = ...) { ... }
UNION
$result