简体   繁体   中英

Inserting and retrieving server variables using PHP and MySQL

What is the best way to insert the $_SERVER array into MySQL and then select it and restore it back into a PHP array?

Since there is a large and expanding list of $_SERVER array elements, I would firstly like to serialize the complete array and store the data in MySQL as type "text". This will avoid many NULL values if individual element are stored in separate fields. As it is I just use one text field. I think this is more efficient.

When I use json_encode to store the array in MySQL and then json_decode when restoring it, I have to escape slashes using str_replace.

Using str_replace is cumbersome and strikes me that this may lead to problems if I overlook characters which need escaping. There must be a better way.

Please note that all data which I store in MySQL always gets passed through the mysql_real_escape_string function first and this is happening here too.

use serialize() and unserialize() instead.

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

(use prepared statements from mysqli or PDO, as data may get crippled by escaping text)

(and it is better idea to use MySQL datatype BLOB instead of TEXT)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM