0

我正在尝试将 MySQL 数组中的结果放入另一个 MySQL 表中,但是以特定的静态顺序。

这是我的数组(MySQL):

Array
(
    [1] => Array
        (
            [id] => 19
            [user_id] => 18
            [size] => Large
        )

    [2] => Array
        (
            [id] => 18
            [user_id] => 17
            [size] => Large
        )

    [3] => Array
        (
            [id] => 11
            [user_id] => 8
            [size] => Large
        )

    [4] => Array
        (
            [id] => 7
            [user_id] => 2
            [size] => Large
        )

    [5] => Array
        (
            [id] => 3
            [user_id] => 1
            [size] => Large
        )

    [6] => Array
        (
            [id] => 16
            [user_id] => 16
            [size] => Small
        )

    [7] => Array
        (
            [id] => 15
            [user_id] => 15
            [size] => Small
        )

    [8] => Array
        (
            [id] => 14
            [user_id] => 14
            [size] => Small
        )

    [9] => Array
        (
            [id] => 10
            [user_id] => 4
            [size] => Small
        )

    [10] => Array
        (
            [id] => 8
            [user_id] => 3
            [size] => Small
        )

    [11] => Array
        (
            [id] => 4
            [user_id] => 1
            [size] => Small
        )

    [12] => Array
        (
            [id] => 17
            [user_id] => 16
            [size] => Wide
        )

    [13] => Array
        (
            [id] => 12
            [user_id] => 12
            [size] => Wide
        )

    [14] => Array
        (
            [id] => 13
            [user_id] => 13
            [size] => Tall
        )

    [15] => Array
        (
            [id] => 5
            [user_id] => 1
            [size] => Tall
        )

)

我现在正在尝试使用此数组输出将特定顺序的数据强制到另一个表中。

这是我所拥有的:

$mysql_array = $mySQLi->DoSelect("
    (SELECT id, user_id, size FROM `table1` WHERE size = 'Large' GROUP BY user_id ORDER BY id DESC LIMIT 0,5)
    UNION ALL
    (SELECT id, user_id, size FROM `table1` WHERE size = 'Small' GROUP BY user_id ORDER BY id DESC LIMIT 0,6)
    UNION ALL
    (SELECT id, user_id, size FROM `table1` WHERE size = 'Wide' GROUP BY user_id DESC ORDER BY id DESC LIMIT 0,2)
    UNION ALL
    (SELECT id, user_id, size FROM `table1` WHERE size = 'Tall' GROUP BY user_id DESC ORDER BY id DESC LIMIT 0,4)
");

for($i = 1; $streamItems[$i] = $mySQLi->GetArray($mysql_array); $i++)
{
    // Large images
    if($i === 1  && $streamItems[$i]['size'] == 'Large') { saveToNewTable(1, $streamItems[$i]['id'], $streamItems[$i]['user_id'],  1); }
    if($i === 2  && $streamItems[$i]['size'] == 'Large') { saveToNewTable(1, $streamItems[$i]['id'], $streamItems[$i]['user_id'],  4); }
    if($i === 3  && $streamItems[$i]['size'] == 'Large') { saveToNewTable(1, $streamItems[$i]['id'], $streamItems[$i]['user_id'], 10); }
    if($i === 4  && $streamItems[$i]['size'] == 'Large') { saveToNewTable(1, $streamItems[$i]['id'], $streamItems[$i]['user_id'], 11); }
    if($i === 5  && $streamItems[$i]['size'] == 'Large') { saveToNewTable(1, $streamItems[$i]['id'], $streamItems[$i]['user_id'], 12); }

    // Small
    if($i === 6  && $streamItems[$i]['size'] == 'Small') { saveToNewTable(1, $streamItems[$i]['id'], $streamItems[$i]['user_id'],  2); }
    if($i === 7  && $streamItems[$i]['size'] == 'Small') { saveToNewTable(1, $streamItems[$i]['id'], $streamItems[$i]['user_id'],  3); }
    if($i === 8  && $streamItems[$i]['size'] == 'Small') { saveToNewTable(1, $streamItems[$i]['id'], $streamItems[$i]['user_id'],  6); }
    if($i === 9  && $streamItems[$i]['size'] == 'Small') { saveToNewTable(1, $streamItems[$i]['id'], $streamItems[$i]['user_id'],  7); }
    if($i === 10 && $streamItems[$i]['size'] == 'Small') { saveToNewTable(1, $streamItems[$i]['id'], $streamItems[$i]['user_id'], 13); }
    if($i === 11 && $streamItems[$i]['size'] == 'Small') { saveToNewTable(1, $streamItems[$i]['id'], $streamItems[$i]['user_id'], 14); }

    // Wide
    if($i === 12 && $streamItems[$i]['size'] == 'Wide')  { saveToNewTable(1, $streamItems[$i]['id'], $streamItems[$i]['user_id'],  5); }
    if($i === 13 && $streamItems[$i]['size'] == 'Wide')  { saveToNewTable(1, $streamItems[$i]['id'], $streamItems[$i]['user_id'], 17); }

    // Tall
    if($i === 14 && $streamItems[$i]['size'] == 'Tall')  { saveToNewTable(1, $streamItems[$i]['id'], $streamItems[$i]['user_id'],  8); }
    if($i === 15 && $streamItems[$i]['size'] == 'Tall')  { saveToNewTable(1, $streamItems[$i]['id'], $streamItems[$i]['user_id'],  9); }
    if($i === 16 && $streamItems[$i]['size'] == 'Tall')  { saveToNewTable(1, $streamItems[$i]['id'], $streamItems[$i]['user_id'], 15); }
    if($i === 17 && $streamItems[$i]['size'] == 'Tall')  { saveToNewTable(1, $streamItems[$i]['id'], $streamItems[$i]['user_id'], 16); }
}

function saveToNewTable($stream_id, $artwork_id, $user_id, $sequence) 
{
    global $mySQLi;

    $mySQLi->DoSql("
        INSERT INTO 
            table2 
        (
            stream_id, 
            artwork_id, 
            user_id, 
            sequence
        ) 
        VALUES 
        (
            ".$stream_id.", 
            ".$artwork_id.", 
            ".$user_id.", 
            ".$sequence."
        )
    ");
}

发生的情况是,只有 $mysql_array 的第一个结果会被插入到新表中——但是 3 次。为什么是3次?

我还收到以下警告:警告:mysqli_fetch_assoc() 期望参数 1 为 mysqli_result

干杯

4

0 回答 0