5 Years
Discussion Span
Last Post by diafol

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.


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); 

Edited by diafol

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.