将多维数组插入mysql

发布于 2024-11-25 16:13:29 字数 996 浏览 0 评论 0原文

我想将多维数组中的数据插入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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

伤痕我心 2024-12-02 16:13:29

您可以尝试序列化它们并将它们存储为 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.

可爱咩 2024-12-02 16:13:29

如果您不需要搜索数据,序列化数组实际上并不是最糟糕的主意。另一种选择是为数据选择另一种数据结构,例如二叉树和对象。您可以序列化和反序列化该对象,并且因为它是二叉树,所以您可以在内存中非常快速地搜索它。这实际上是我自己想到的一件事。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文