is it possible to use a string array in mysql using the set data type and add a valuse to it, like a list of names?

Recommended Answers

All 2 Replies

Member Avatar for LastMitch

is it possible to use a string array in mysql using the set data type and add a valuse to it, like a list of names?

Yes, As long you can INSERT the data in the database.

Do you have a code that you can show to see what you have?

Kinda like a query to see what you are doing and talking about.

Member Avatar for diafol

Not sure what you mean here. Have you looked at the mysql manual?
The SET syntax limits you to inserting one record at a time, whereas the VALUES syntax allows you to insert multiple records. AFAIK.

INSERT INTO table SET field1='value1', field2='value2'

INSERT INTO table (field1, field2) VALUES ('value1', 'value2') //INSERTS 1 record

INSERT INTO table (field1, field2) VALUES (('value1', 'value2'),('value3', 'value4')) // iNSERTS 2 recs etc

If you have an array, you can often include this into a VALUES SYNTAX sql with little trouble:

$r = array('value1', 'value2');

$sql = "INSERT INTO table (field1, field2) VALUES ("' . implode("','", $r) . ")";

If you have a multidimensional array (multiple records):

$r = array(array('value1', 'value2', 'value3'),array('value4', 'value5', 'value6'));
$str = array();
foreach($r as $record) $str[] = "('" . implode("','", $r) . "')";

$sql = "INSERT INTO table (field1, field2, field3) VALUES (" . implode(",", $str) . ")";

I'm sure there's a more succinct way, but it's getting late :)

FOR set - if keys are the same as the field names:

$r = array('field1' => 'value1', 'field2' => 'value2');
$str = array();
foreach($r as $k=>$v) $str[] = "`$k` = '$v'";

$sql = "INSERT INTO table SET " . implode(',',$str); 
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.