简体   繁体   English

MySQL PHP将两行数据插入多维数组

[英]MySQL PHP insert data from two rows into multidimensional array

I am trying to insert data into a multidimensional array from two tables in my database, but it does something that I don't really know how to fix. 我正在尝试从数据库中的两个表中将数据插入多维数组,但是它确实不知道如何解决。

First of all, here is my PHP script: 首先,这是我的PHP脚本:

if ($stmt = $mysqli->prepare('SELECT objects_price.object_id, materials.material_id, materials.name, material_amount
                              FROM objects_price
                              INNER JOIN materials ON objects_price.material_id=materials.material_id
                             ')) {
    $stmt->execute();

    $stmt->bind_result($object_id, $material_id, $material_name, $material_amount);
    $j = 1;
    $arr = array();

    while ($stmt->fetch()) {
        $k = 1;
        if ($object_id == $k) {
            $arr[] = array($j, $material_name, $material_amount);
            $k++;
            $j++;
        }
    }

    $stmt->close();

This script takes out some values from the database and inserts them in the array. 该脚本从数据库中取出一些值,并将它们插入数组中。 My database looks like this: (objects_price) 我的数据库如下所示:(objects_price)

这是objects_price

(materials) (材料)

这是材料

What happens right now is that it only outputs: 现在发生的是它仅输出:

上面的代码输出什么

If I change the database query to: 如果我将数据库查询更改为:

if ($stmt = $mysqli->prepare('SELECT objects_price.object_id, materials.material_id, materials.name, material_amount
                            FROM objects_price
                            INNER JOIN materials
                            ')) {

it outputs this: 它输出以下内容:

仅使用INNER JOIN材料

So the left side of this table is shown correct, but the right side shows only the first row of the price, (47). 因此,该表的左侧显示为正确,但右侧仅显示了价格的第一行(47)。 It should look like this: 它看起来应该像这样:

在此处输入图片说明

I don't know if this is easily achieveable by changing the database query. 我不知道是否可以通过更改数据库查询轻松实现。 But I hope I can get some advice, suggestions or help in here. 但我希望我能在这里得到一些建议,建议或帮助。 Thanks in advance. 提前致谢。

Try using a left join for your table: 尝试对表使用左联接:

$query = "SELECT m.name, op.objects_id
    FROM objects_price op 
    LEFT JOIN materials m USING(material_id)";
$result = $mysqli->query($query);

The next thing you need to change is the way you handle the results. 接下来需要更改的是处理结果的方式。 It's quite possible you're going through the results and skipping many, which is why you only end up with one. 您很有可能正在浏览结果并跳过许多结果,这就是为什么只得到一个结果的原因。 Try this: 尝试这个:

$arr = array();
while($row = $result->fetch_assoc()) {
    $arr[$row['objects_id']] = $row['name'];
}

If you have more than 1 material per object then just use this slight adjustment for a multidimensional array 如果每个对象的材料超过一种,则只需对多维数组使用此轻微调整

$arr = array();
while($row = $result->fetch_assoc()) {
    $arr[$row['name']][] = $row['objects_id'];
}

Then to produce your table from this: 然后从此生成表:

echo '<table>';
foreach($arr as $name => $objects) {
    echo '<tr><td>' . $name . '</td><td>';
    $space = '';
    foreach($objects as $id) {
        echo $space . $id;
        $space = ", ";
    }
    echo '</td></tr>';
}
echo '</table>';

A few tips: 一些提示:

  1. if you're not putting any variables in, you don't need to prepare the query. 如果您不输入任何变量,则无需准备查询。 You can just run it as above. 您可以像上面一样运行它。
  2. Don't select more columns than you need. 不要选择过多的列。 Why grab all columns from the table when you only use 2? 当仅使用2时,为什么要从表中获取所有列?
  3. if you're joining on a column that has the same name in both tables you can use USING(column_name) . 如果您要加入两个表中具有相同名称的列,则可以使用USING(column_name)

Everything looks good in your code except for these lines: 除以下几行外,所有内容在您的代码中看起来都不错:

 while ($stmt->fetch()) {
    $k = 1;

What is happening is that $k is being declared and given the value of 1. Because it is in the loop, each time that line is run the value goes back to 1. To solve this simply put that value outside the loop like this: 发生的事情是$ k被声明并赋予值1。因为它在循环中,所以每次运行该行时,该值都返回1。要解决此问题,只需将值放在循环外,如下所示:

 $k = 1;
 while ($stmt->fetch()) {
     //blah blah blah
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM