首页 \ 问答 \ 将数组存储在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
更新时间:2021-06-18 20:06

最满意答案

您可以使用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

相关问答

更多

相关文章

更多

最新问答

更多
  • Google会更新重定向网址吗?(Does Google update redirect URLs? [closed])
  • 如何将js-variable从.js文件传递给Electron中的.html页面?(How to pass js-variable from .js file to .html page in Electron?)
  • matlab中的数据处理gui - 从文件和文本框中读取并传递给函数(data handling in matlab gui - reading from file and textbox and pass to function)
  • SVG矩形的笔划宽度在顶部/左侧被切除。(SVG rectangle's stroke widths are cut off on top/left. How do I fix this?)
  • BOOST_CHECK中的错误?(Bug in BOOST_CHECK?)
  • 使用ActiveDocument时记录替换量(Recording the amount of replacements when using ActiveDocument)
  • 将php数组转换为javascript数组(convert php array to a javascript array)
  • 在斯威夫特干邑国际象棋引擎与iOS项目集成(Stockfish Chess Engine Integration with iOS project in Swift)
  • C# - 增加一个双倍值(1.212E + 25)(C# - Incrementing a double value (1.212E+25))
  • 取消asynctask的最佳方法(Best way to cancel asynctask)
  • 文件转换问题的最佳方法(Best approach to File Conversion problems)
  • 从分组数据中选择第一行和最后一行(Select first and last row from grouped data)
  • 哪些元素可以禁用?(What elements can ng-disabled be applied to?)
  • ASP.NET GridView在回发时为空(ASP.NET GridView empty on postback)
  • lua环境和模块(lua environments and modules)
  • 如何将小切片复制到更大切片的末尾(How to copy a small slice to the end of a bigger slice)
  • IIS7 URL重写模块替换(IIS7 URL Rewriting Module Replace)
  • IE和Google Chrome中的多表单提交(Multi-forms submission in IE and Google chrome)
  • Symfony2:如何在控制器中检索UserChecker?(Symfony2: How to retrieve the UserChecker in a controller?)
  • 循环通过数组和删除项目,而不破坏循环(Looping through array and removing items, without breaking for loop)
  • 如何动态更改YouTube播放器的选项(How to dynamically change YouTube player's options)
  • Laravel使用以点开头的文件处理存储磁盘(Laravel working through storage disk with files starting with dot)
  • 在挂架中使用重定向时出错(Error using redirect in pylons)
  • 替换netCDF文件中的变量(Replacing a variable inside a netCDF file)
  • 让swable中的非可变数组(let Non-mutable array in swift)
  • 春季安全缺省登录页面代码位于何处?(Where is spring security default login page code located?)
  • Unix - 并排打印公共线(Unix - Print Common Lines side by side)
  • 验证TimeSpan(Validating TimeSpan)
  • 如果还有一个具有返回值的重载版本,如何正确调用非返回值Swift函数?(How do I properly call a non-return-value Swift function when it also has an overloading version that has returned value?)
  • Perl:比较两个文件并打印匹配和不匹配的数据(Perl: Comparing two files and printing data that match and don't match)