Array data type in mysql

I have an array type variable and I want to save it as it is in the database (mysql), and when I use tinyblob, which I usually use in java to display, it returns abnormal output. So what is the correct data type for an array in php.

$countryGoup=array("USA","FRANCE","GERMANY","BRITAIN");
+5
source share
1 answer

You can:

1) use the SET data type if the values ​​are multiple and fixed

You define the column in the expression CREATE TABLEas SET('USA', 'France', 'Germany', ...) NOT NULL(not null optional) and use queries such as:

INSERT INTO myTable SET countries="USA,Germany";

2) use a lookup table with an external link

So you define the table countries:

id_country|name
----------+-----------
1         |USA
2         |Germany
3         |France

, (f.ex. "users" ) , --.

F.ex., a users_countries users countries :

id_user|id_country
------------------
1      |1
1      |2
2      |1

1 1 2 ( ), 2 ..


3) -

( PHP) (, , pipe |):

$countries = array("USA", "Russia", "Iran");
$countriesString = implode (",", $countries); // $countriesString = "Usa,Russia,Iran"
$query = "INSERT INTO mytable SET countries = '" . $countriesString . "'";

(: , , , )

db, , , $array = explode(",", $retrievedValueFromDb), .

Update:

4) find_in_set() MySQL

find_in_set(needle, field), , , .

, 1) , "USA,Germany", , USA, :

SELECT * FROM myTable WHERE find_in_set('USA', countries) <> 0

1) , , . , 64 , , (,), .

2) ,

3) , SQL

, dbms, .

+12

All Articles