如何在MySQL数据库中插入PHP多维数组
我不明白如何将PHP多维数组中的数据插入MySQL数据库 以下是我的页面中的代码:如何在MySQL数据库中插入PHP多维数组,php,mysql,multidimensional-array,Php,Mysql,Multidimensional Array,我不明白如何将PHP多维数组中的数据插入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_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}'"));
}
}
}
字段“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>";
}
。
.
感谢您的回复。您是否发布了您的查询尝试?我不确定我的语法是否正确,但如果下面的代码片段有效,则会更加枯燥。我见过很多关于堆栈溢出的狂野、不守规则的代码,但这段代码完全是狂暴的。这在几个层面上都是错误的,但主要是因为PHP不是花哨的shell脚本,所以不要这样对待它。尽可能使用本机函数。这会无缘无故地产生大量的shell调用,这会造成巨大的安全风险。请使用诸如处理文件之类的函数。用于在使用拆分行后,找出文件中有多少行。如果您使用的是PHP,请使用PHP。您是否发布了您的查询尝试?我不确定我的语法是否正确,但如果下面的代码段有效,则会更加枯燥。我见过很多关于堆栈溢出的狂野、不守规则的代码,但这段代码完全是狂暴的。这在几个层面上都是错误的,但主要是因为PHP不是花哨的shell脚本,所以不要这样对待它。尽可能使用本机函数。这会无缘无故地产生大量的shell调用,这会造成巨大的安全风险。请使用诸如处理文件之类的函数。用于在使用拆分行后,找出文件中有多少行。如果您使用的是PHP,请使用PHP。
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