将数组存储在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 wordArray
.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
最满意答案
您可以使用serialize()函数序列化您的数组。
例:
serialize($event_items);
生成值的可存储表示。
这对于存储或传递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.
相关问答
更多-
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
如何从数据库中读取数据并使用linq to sql将其存储在数组中?(How to read data from database and store it in array using linq to sql?)[2022-06-22]
只需调用扩展方法ToArray() : var array = (from p in db.table1 select p.id).ToArray(); Just call an extension method ToArray(): var array = (from p in db.table1 select p.id).ToArray(); -
如果将此数据转换为图像,则会丢失某些信息(哪一点属于哪一行)。 如果要保留此信息,则应序列化数据。 决定格式 :你可以使用像XML这样非常冗长的东西......
... ... -
将数组存储在sql数据库中(storing array in a sql database)[2022-02-27]
您可以使用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 st ... -
连接代码后添加这一行 if($db->connect_errno) { echo "Error: ( " .$db->errorno. " )". $db->error; die; } 只需替换此代码即可 if (count($errors) == 0) { $password = md5($password_1);//encrypt the password before saving in the database $query = "INSERT INTO use ...
-
听起来你想要这样的东西? string[] tasks; string sql = "SELECT [Task Name] FROM Tasks"; using (OleDbCommand cmd = new OleDbCommand(sql, conn)) { using (OleDbDataReader dataReader = cmd.ExecuteReader()) { ...
-
我猜你有一个MySql,关系数据库。 作为第一种方法 ,您必须考虑在关系数据库 的字段中插入任何类型的组合数据 (CSV,JSON, serialize() ), 这是您应始终应避免的 。 这是我在大学学习数据库时学到的第一件事。 这是因为当您设计数据库时,您的第一个方法应该是数据库规范化 。 非规范化是在寻找性能时常用的东西。 为此,您需要在数据库(建模,访问等)方面拥有丰富的经验。 这是经验丰富的DBA和商业智能专业人士所做的事情,但如果你真的不知道自己在做什么,那么你不必尝试。 所以,你的目标是设计一 ...
-
对象到字符串: 您可以将对象作为JSON字符串存储在DB中。 这是一个简单的例子: var foo = new Object(); foo.Name = "James"; foo.Gender = "Male"; //Result: {"Name":"James","Gender":"Male"} var stringRepresentation = window.JSON.stringify(foo); 这是一个工作小提琴 。 字符串到对象: 要将字符串转换回对象,只需调用window.JSON.pa ...
-
我认为不一定有标准的方法,但对我来说似乎很明显和简单的是一个基本上多对多的表(尽管你的css变量名不一定是独立存储在数据库中,除非你需要添加比变量名更多的数据): 用户名 css_variable_name_or_id(如果它引用css值的sql表,则为id) 值 这样,您可以轻松地为用户选择所有css,或者为用户单独选择user_id和css_variable_name。 至少在user_id上放一个索引。 这应该会使性能得到快速提升。 (由于所有重复项,您可能不希望在css_variable_name上 ...
-
使用SQL函数将与使用相同SQL引擎的其他工具更兼容。 使用ISO格式的文本YYYY-MM-DD hh:mm:ss.fff将广泛兼容并仍然可以正确排序。 Using the SQL functions will be more compatible with other tools using the same SQL engine. Using text in ISO format YYYY-MM-DD hh:mm:ss.fff will be widely compatible and still so ...