首页 \ 问答 \ 将数组存储在sql数据库中(storing array in a sql database)

将数组存储在sql数据库中(storing array in a sql database)

我开始进入数组并且不能让它运行良好。 我习惯使用explode / implode函数,但是我在这部分代码中使用数组会让我的生活变得更轻松。 这是一个叫做的函数:

function save_event($event_items = NULL) {
include 'connect.php';

$now = date("Y-m-d H:i:s");
$sqla = "
INSERT INTO `event`(`event_items`, `event_entered`)
    VALUES ('$event_items','$now')";

    $resulta = mysqli_query($link, $sqla);
    if(!$resulta)
        {
        echo '<br/>An error occurred while inserting your data. Please try again later.<br/>';
        }
    else
        { echo 'this is the variable to be stored:<br/>';
          print_r($event_items);

          $sql = "SELECT * FROM `event` WHERE event_entered = '".$now."'";
          $result = mysqli_query($link, $sql);

          if($result)
            { while($row = mysqli_fetch_assoc($result))
                  { echo '<br/></br>This is the value of the database:<br/>';
                    print_r ($row['event_items']);
                  }
            }
          }
}

此功能打印:

this is the variable to be stored:
Array( [0] => Array ( [item] => Powered Speaker [note] => [quantity] => 2 [price] => 200.00 [category] => Audio ) [1] => Array ( [item] => Wireless Microphone [note] => Lavalier [quantity] => 3 [price] => 175.00 [category] => Audio ))

This is the value of the database:
Array

在phpMyAdmin中,我在event_items列中event_items只是单词Array

附加信息:我有一个名为Groups的表,每个组将有一个或多个订单(另一个名为Order的表),每个订单也将有一个或多个事件(另一个表)。 最后,每个事件将包含一个或多个项目(每个项目及其相应的价格,数量,注释和类别),这些项目存储在事件表中的一个(或多个)列中。


I started getting into arrays and don't quite get it to work well. I'm used to work with explode/implode functions but I though arrays would make my life easier in this part of the code. Here is the function called:

function save_event($event_items = NULL) {
include 'connect.php';

$now = date("Y-m-d H:i:s");
$sqla = "
INSERT INTO `event`(`event_items`, `event_entered`)
    VALUES ('$event_items','$now')";

    $resulta = mysqli_query($link, $sqla);
    if(!$resulta)
        {
        echo '<br/>An error occurred while inserting your data. Please try again later.<br/>';
        }
    else
        { echo 'this is the variable to be stored:<br/>';
          print_r($event_items);

          $sql = "SELECT * FROM `event` WHERE event_entered = '".$now."'";
          $result = mysqli_query($link, $sql);

          if($result)
            { while($row = mysqli_fetch_assoc($result))
                  { echo '<br/></br>This is the value of the database:<br/>';
                    print_r ($row['event_items']);
                  }
            }
          }
}

This function prints:

this is the variable to be stored:
Array( [0] => Array ( [item] => Powered Speaker [note] => [quantity] => 2 [price] => 200.00 [category] => Audio ) [1] => Array ( [item] => Wireless Microphone [note] => Lavalier [quantity] => 3 [price] => 175.00 [category] => Audio ))

This is the value of the database:
Array

In phpMyAdmin, all I see in the column event_items is the word Array.

Additional info: I have a table called Groups, each group will have one or multiple orders (another table called Order) and each order will have also one or multiple events (another table). Lastly, each event will have one or multiple items (each item with its corresponding price, quantity, note and category), which are stored in one (or many) columns in the Event table.


原文:https://stackoverflow.com/questions/17223625
更新时间:2022-02-27 15:02

最满意答案

您可以使用serialize()函数序列化您的数组。

例:

serialize($event_items);

生成值的可存储表示。

这对于存储或传递PHP值非常有用,而不会丢失其类型和结构。

http://php.net/manual/en/function.serialize.php


You could serialize your array with the serialize() function.

Example:

serialize($event_items);

Generates a storable representation of a value.

This is useful for storing or passing PHP values around without losing their type and structure.

http://php.net/manual/en/function.serialize.php

相关问答

更多