如何在MySQL数据库中插入PHP多维数组

时间:2017-10-23 00:01:35

标签: php mysql multidimensional-array

我不明白如何将PHP multi0dimensional数组中的数据插入到MySQL数据库中。

以下是我页面中的代码:

$stato_porte = shell_exec("porte_lista.sh > ../tmp/stato_porte.txt");
$numero_porte = (int)shell_exec("cat ../tmp/stato_porte.txt | grep 'Giga\|Vlan' | wc -l");

for ($i = 1; $i <= $numero_porte; $i++)
{
    $stato_porta[$i] = trim(shell_exec(" cat ../tmp/stato_porte.txt | grep 'Giga\|Vlan' | awk  'FNR == ".$i." {print  $1}'"));
    if ($stato_porta[$i] == "*") 
    {
        $interfaccia[0][$i] = trim(shell_exec(" cat ../tmp/stato_porte.txt | grep 'Giga\|Vlan' | awk  'FNR == ".$i." {print  $2}'"));
        for ($a = 1; $a <= 3; $a++)
        {
            $interfaccia[1][$i] = trim(shell_exec(" cat ../tmp/stato_porte.txt | grep 'Giga\|Vlan' | awk  'FNR == ".$i." {print  $7}'"));
            $interfaccia[2][$i] = trim(shell_exec(" cat ../tmp/stato_porte.txt | grep 'Giga\|Vlan' | awk  'FNR == ".$i." {print  $9}'"));
        }
    } 
    else 
    {
        $interfaccia[0][$i] = trim(shell_exec(" cat ../tmp/stato_porte.txt | grep 'Giga\|Vlan' | awk  'FNR == ".$i." {print  $1}'"));
        for ($a = 1; $a <= 3; $a++)
        {
            $interfaccia[1][$i] = trim(shell_exec(" cat ../tmp/stato_porte.txt | grep 'Giga\|Vlan' | awk  'FNR == ".$i." {print  $6}'"));
            $interfaccia[2][$i] = trim(shell_exec(" cat ../tmp/stato_porte.txt | grep 'Giga\|Vlan' | awk  'FNR == ".$i." {print  $8}'"));
        }
    }
}

在多维数组“$ interfaccia”中,我将位置[0] [n]放在开关的所有接口名称中,在位置[1] [n]中,我将所有接口的所有rx状态放在位置[2] [n]我把所有接口的所有tx状态。

在mysql中我有这个表:

MariaDB [network_devices]> describe data_interfaces;
+-------------+-----------------+------+-----+-------------------+-----------------------------+
| Field       | Type            | Null | Key | Default           | Extra                       |
+-------------+-----------------+------+-----+-------------------+-----------------------------+
| id          | int(6) unsigned | NO   | PRI | NULL              | auto_increment              |
| device      | varchar(30)     | NO   |     | NULL              |                             |
| interface   | varchar(30)     | NO   |     | NULL              |                             |
| tx          | varchar(50)     | YES  |     | NULL              |                             |
| rx          | varchar(50)     | YES  |     | NULL              |                             |
| reg_date    | timestamp       | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |

使用静态变量插入字段“device”但是如何从多维数组中插入数据?

为了更清楚,如果我通过代码打印数组:

for ($b = 1; $b <= $numero_porte; $b++)
{
    echo "Interfaccia: ".$interfaccia[0][$b];
    echo "<br>";
    echo "TX: ".$interfaccia[1][$b];
    echo "<br>";
    echo "RX: ".$interfaccia[2][$b];
    echo "<br>";
}

输出是:

Interfaccia: Vlan1
TX: 0
RX: 0
Interfaccia: Vlan7
TX: 0
RX: 0
Interfaccia: Vlan70
TX: 0
RX: 0
Interfaccia: Vlan1000
TX: 0
RX: 0
Interfaccia: Vlan2012
TX: 1000
RX: 1000
Interfaccia: GigabitEthernet1/0/1
TX: 5542000
RX: 5584000
Interfaccia: GigabitEthernet1/0/2
TX: 0
RX: 9000
Interfaccia: GigabitEthernet1/0/3
TX: 0
RX: 1000
Interfaccia: GigabitEthernet1/0/4
TX: 0
RX: 1000

。 。

感谢您的回复。

0 个答案:

没有答案