1,105,344 Community Members

Insert into set question?

Member Avatar
CreatorZeus
Newbie Poster
22 posts since Aug 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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?

LastMitch
Deleted Member
 
1
 

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
diafol
Where are my eyes?
12,977 posts since Oct 2006
Reputation Points: 1,821 [?]
Q&As Helped to Solve: 1,848 [?]
Skill Endorsements: 92 [?]
Moderator
Featured
Sponsor
 
1
 

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); 
Question Answered as of 1 Year Ago by diafol and LastMitch
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: