We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,617 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Insert into set question?

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?

3
Contributors
2
Replies
10 Hours
Discussion Span
3 Months Ago
Last Updated
14
Views
Question
Answered
CreatorZeus
Newbie Poster
6 posts since Aug 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 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?

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.

LastMitch
Industrious Poster
4,374 posts since Mar 2012
Reputation Points: 149
Solved Threads: 349
Skill Endorsements: 47

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); 
diafol
Keep Smiling
Moderator
10,826 posts since Oct 2006
Reputation Points: 1,675
Solved Threads: 1,532
Skill Endorsements: 61
Question Answered as of 3 Months Ago by diafol and LastMitch

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.0609 seconds using 2.69MB