How to store PHP array into database

不言
Release: 2023-03-31 13:56:01
Original
6547 people have browsed it

This article mainly introduces how to store PHP arrays into the database. It has certain reference value. Now I share it with you. Friends in need can refer to it

Get to the point

It turns out that array array cannot be directly inserted into the database. You have to use the json_encode() method, and the database columns must be set to text

Step 1: Change the database columns ( Field) is set to text

Type Size Use
TINYTEXT 0-255 bytes Short text string
TEXT 0-65 535 bytes Long text data
MEDIUMTEXT 0-16 777 215 bytes Medium length text data
LONGTEXT 0-4 294 967 295 bytes Very large text data

There are 4 TEXT types: TINYTEXT, TEXT, MEDIUMTEXT and LONGTEXT. The corresponding four BLOB types have different maximum storage lengths, which can be selected according to the actual situation.

The second step: json encoding of variables

json_encode(value)
Copy after login

value: array variable

The third step: read from the database and decode json

json_decode(value)
Copy after login

value:Variable

JSON function

##FunctionDescriptionjson_encodeEncode variables in JSONjson_decodeDecode strings in JSON format and convert them into PHP variables

The above is the entire content of this article. I hope it will be helpful to everyone’s study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

Summary of the cheat sheet in PHP strings

How PHP dynamically modifies the configuration file

How to use PHP to make the SWOOLE extension implement scheduled synchronization of MySQL data

The above is the detailed content of How to store PHP array into database. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template