Reputation: 477
I have one array that is filled by mysql_query. Values that are in this array I want to put into one continuous string for example:
$array = array(1, 2, 3, 4, 5);
INSERT INTO 'new_slovmas'.'$tabulka'('$array[0]', '$array[1]', '$array[3]')
VALUES ('$r_1', '$r_2', '$r_3')";
Sense in one table is over 50 columns I do not want to fill it manually, but by WHILE or FOR. So I was thinking about putting values into one string like this:
$array = array("'1',", "'2',", "'3',", "'4',", "'5',");
so now I have this:
echo $array[2]; => '3',
By the cycle I wont to achieve having multiple entries in one variable.
$str = $array[0], $array[1] ... $array[xy];
INSERT INTO 'new_slovmas'.'$tabulka'('$str')
VALUES ('$r_1', '$r_2', '$r_3')";
Upvotes: 2
Views: 468
Reputation: 15338
PHP's implode function will unpack an array into a string for use in Javascript. Try this:
$str = implode(',',$array);
Upvotes: 3
Reputation: 6275
Use:
join(",", $array);
Also, be sure to escape your values before you insert them into the database. You do this in case that one of your values has a single quote (') or something worse in it (like valid SQL), which would crash your query. You can use mysql_real_escape_string(...) or mysqli_real_escape_string(...) for this
Upvotes: 0
Reputation: 219814
Use implode()
$string = implode(',', array(1, 2, 3, 4, 5)); // $string = 1,2,3,4,5
Upvotes: 5