将多维数组插入mysql
我想将多维数组中的数据插入mysql,但在循环时循环次数超出了必要的范围。我的意思是我希望它输入 6 条记录,但我有很多记录。我知道我的 mysql 查询位置有问题,但尝试了很多技术,但都无效。这是我的代码 请帮我
<?php
$primary = array(
"array 1"=> array("clas" => "val 1","pupil" => "val 2","subject" => "val 3"),
"array 2 "=> array("clas" => "val 1","pupil" => "val 2","subject" => "val 3"),
"array 3" => array("clas" => "val 1","pupil" => "val 2","subject" => "val 3"),
"array 4" => array("clas" => "val 1","pupil" => "val 2","subject" => "val 3"),
"array 5" => array("clas" => "val 1","pupil" => "val 2","subject" => "val 3"),
"array 6" => array("clas" => "val 1","pupil" => "val 2","subject" => "val 3"),
);
foreach($primary as $t => $value){
$class =$t;
$clas = $primary[$t]["clas"];
$pupil = $primary[$t]["pupil"];
$sub =$primary[$t]["subject"];
mysql_query("insert into tablename( f1, f2, f3) values('$clas','$pupil','$sub')");
}
i want to insert data from multidimensional array into mysql but while looping it loop more than necessary. i mean i want it to enter 6 records but i got many records. i knew i have problem with my mysql query location but have tried many techniques but all in void. this is my code
please help me
<?php
$primary = array(
"array 1"=> array("clas" => "val 1","pupil" => "val 2","subject" => "val 3"),
"array 2 "=> array("clas" => "val 1","pupil" => "val 2","subject" => "val 3"),
"array 3" => array("clas" => "val 1","pupil" => "val 2","subject" => "val 3"),
"array 4" => array("clas" => "val 1","pupil" => "val 2","subject" => "val 3"),
"array 5" => array("clas" => "val 1","pupil" => "val 2","subject" => "val 3"),
"array 6" => array("clas" => "val 1","pupil" => "val 2","subject" => "val 3"),
);
foreach($primary as $t => $value){
$class =$t;
$clas = $primary[$t]["clas"];
$pupil = $primary[$t]["pupil"];
$sub =$primary[$t]["subject"];
mysql_query("insert into tablename( f1, f2, f3) values('$clas','$pupil','$sub')");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试序列化它们并将它们存储为 blob。序列化会将它们变成一个巨大的字节数组,您可以检索和反序列化。
You might try serializing them and storing them as a blob. Serializing would turn them into a giant array of bytes that you could retrieve and unserialize.
如果您不需要搜索数据,序列化数组实际上并不是最糟糕的主意。另一种选择是为数据选择另一种数据结构,例如二叉树和对象。您可以序列化和反序列化该对象,并且因为它是二叉树,所以您可以在内存中非常快速地搜索它。这实际上是我自己想到的一件事。
Serialize the array isn't actually the badest idea if you don't need to search for the data. Another option is to choose another data structure for the data for example a binary tree and an object. The object you can serialze and unserialize and because it's a binary tree you can search it very fast in memory. It's a thing I actually think of myself.