开发者

Storing an array (doubles) in phpMyAdmin

I'm very new to MySQL, although I've used SQL databases in other contexts before. I have a test site set up which has an online cPanel with access to phpMyAdmin. I'm attempting to setup a MySQL database, and so far it's working fine (I can connect to the Database 开发者_如何学Cand the table).

The only problem I'm having is with inserting data. I'd like to insert an entire array (specifically, the array will be a double[]) into one column. After looking at the column types available in phpMyAdmin, it doesn't seem to support inserting arrays other than Binary arrays.

I've found many solutions for inserting arrays programatically including this thread, but for this site we will be inserting data via the online cPanel. Is there a way to do that?


If you want access to that data, and want to be able to use the power of SQL to search in your double[], you should do it this way:

First, you should spend some time researching relational databases. They allow you to create linked data.

An important part of every relational database is using good keys. A key is a unique identifier for a row that allows you to access the data on that row in an efficient manner.

Another important part of relational databases are indexes. Indexes are not required to be unique. But are useful if you are trying to search on them (SQL has made an "index" of the table based on a column or group of columns)

If you wanted to create a table that would have a double[] array, you might instead create a 2nd table that relates to the first table by the first tables primary key.

CREATE TABLE base (
    base_id INT AUTO_INCREMENT,
    name    VARCHAR(32),
    PRIMARY KEY(base_id)
);

CREATE TABLE darray (
    base_id INT,
    data    DOUBLE,
    INDEX(base_id)
);

To get the information back out that you want, you can select using a JOIN statement. If you wanted to get all the information where the base_id was 3, you would write it like so:

SELECT * FROM base
    JOIN darray ON darray.base_id = base.base_id
WHERE base.base_id = 3;

The advanced form of writing this with aliasing

SELECT * FROM base b
    JOIN darray d ON d.base_id = b.base_id
WHERE b.base_id = 3;

If you don't want to have access to the data, but are just recalling it, you should do it this way: (Although this is debatable, I still recommend the above way, if you are willing to learn more sql)

I assume you will be using PHP, we will be serializing the data (see: http://php.net/manual/en/function.serialize.php)

Note we will don't have the darray table, but instead add a

data    BLOB

to the base table.

Inserting with PHP serialized data

<?php
$serializedData = serialize($darray);
$result = mysql_query("INSERT INTO base (name, data) VALUES('a name', '$serializedData ')");

Getting the serialized data

<?php
$result = mysql_query("SELECT data FROM base WHERE base_id=3");
if($result && mysql_affected_rows($result) > 0) {
    $serializedData = mysql_result($result, 0, 'data');
    $darray = unserialize($serializedData);
}


You can import data for tables with a .sql file (basically just a file full of insertion queries) but phpMyAdmin doesn't support inserting data from arbitrary data types. If you want to insert a double[] array as multiple rows in a table, you'll need to take an approach similar to the one in the thread you linked.

(Note that you can always write such a program for the explicit purpose of generating a .sql file which you then use for deployment.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜