“How to concatenate data of two arrays and insert into mysql database?”





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I have two mysql queries to insert the data into two different tables. the result of the queries are as follows:




  1. INSERT INTO table1 (ans1,ans2,ans3,ans4,ans5) VALUES (0,0,0,0,0),(1,1,1,0,0),(0,0,0,0,0)


  2. INSERT INTO table2 (uid,cid,sid) VALUES (1,abc,123),(2,def,456),(3,ghi,789)



How can I concatenate these two results so that the output would look like



INSERT INTO table3 (uid,cid,sid,ans1,ans2,ans3,ans4,ans5)
VALUES
(1,abc,123,0,0,0,0,0),
(2,def,456,1,1,1,0,0),
(3,ghi,789,0,0,0,0,0)









share|improve this question




















  • 3





    There is no real way to do this. You need to provide one or more join columns which relate a record in the first table to one or more records in the second table. SQL is not like Excel, where you can just dump rows and columns willy-nilly.

    – Tim Biegeleisen
    Jan 4 at 6:33











  • If you want, you can create PHP code that inserts values into database and in PHP you can realize inserting value into 3rd table.

    – Hackrrr
    Jan 4 at 6:39











  • "result of the queries"? or are they the queries themselves?

    – TrebledJ
    Jan 4 at 7:32











  • @Hackrrr, basically I do not know how to concatenate the two quires into third table. Could you please suggest the piece of code. I'm showing the out put of what is writing into database. "echo $myquery;"

    – Chaitanya Husys
    Jan 4 at 9:15











  • As an aside, any time you find yourself with enumerated columns (above, say, 2) alarm bells should start ringing. Your table design is likely not optimized for the kinds of queries you'd be likely to run against it.

    – Strawberry
    Jan 4 at 9:45


















0















I have two mysql queries to insert the data into two different tables. the result of the queries are as follows:




  1. INSERT INTO table1 (ans1,ans2,ans3,ans4,ans5) VALUES (0,0,0,0,0),(1,1,1,0,0),(0,0,0,0,0)


  2. INSERT INTO table2 (uid,cid,sid) VALUES (1,abc,123),(2,def,456),(3,ghi,789)



How can I concatenate these two results so that the output would look like



INSERT INTO table3 (uid,cid,sid,ans1,ans2,ans3,ans4,ans5)
VALUES
(1,abc,123,0,0,0,0,0),
(2,def,456,1,1,1,0,0),
(3,ghi,789,0,0,0,0,0)









share|improve this question




















  • 3





    There is no real way to do this. You need to provide one or more join columns which relate a record in the first table to one or more records in the second table. SQL is not like Excel, where you can just dump rows and columns willy-nilly.

    – Tim Biegeleisen
    Jan 4 at 6:33











  • If you want, you can create PHP code that inserts values into database and in PHP you can realize inserting value into 3rd table.

    – Hackrrr
    Jan 4 at 6:39











  • "result of the queries"? or are they the queries themselves?

    – TrebledJ
    Jan 4 at 7:32











  • @Hackrrr, basically I do not know how to concatenate the two quires into third table. Could you please suggest the piece of code. I'm showing the out put of what is writing into database. "echo $myquery;"

    – Chaitanya Husys
    Jan 4 at 9:15











  • As an aside, any time you find yourself with enumerated columns (above, say, 2) alarm bells should start ringing. Your table design is likely not optimized for the kinds of queries you'd be likely to run against it.

    – Strawberry
    Jan 4 at 9:45














0












0








0


1






I have two mysql queries to insert the data into two different tables. the result of the queries are as follows:




  1. INSERT INTO table1 (ans1,ans2,ans3,ans4,ans5) VALUES (0,0,0,0,0),(1,1,1,0,0),(0,0,0,0,0)


  2. INSERT INTO table2 (uid,cid,sid) VALUES (1,abc,123),(2,def,456),(3,ghi,789)



How can I concatenate these two results so that the output would look like



INSERT INTO table3 (uid,cid,sid,ans1,ans2,ans3,ans4,ans5)
VALUES
(1,abc,123,0,0,0,0,0),
(2,def,456,1,1,1,0,0),
(3,ghi,789,0,0,0,0,0)









share|improve this question
















I have two mysql queries to insert the data into two different tables. the result of the queries are as follows:




  1. INSERT INTO table1 (ans1,ans2,ans3,ans4,ans5) VALUES (0,0,0,0,0),(1,1,1,0,0),(0,0,0,0,0)


  2. INSERT INTO table2 (uid,cid,sid) VALUES (1,abc,123),(2,def,456),(3,ghi,789)



How can I concatenate these two results so that the output would look like



INSERT INTO table3 (uid,cid,sid,ans1,ans2,ans3,ans4,ans5)
VALUES
(1,abc,123,0,0,0,0,0),
(2,def,456,1,1,1,0,0),
(3,ghi,789,0,0,0,0,0)






mysql arrays concatenation






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 4 at 6:32









Tim Biegeleisen

239k13100160




239k13100160










asked Jan 4 at 6:30









Chaitanya HusysChaitanya Husys

367




367








  • 3





    There is no real way to do this. You need to provide one or more join columns which relate a record in the first table to one or more records in the second table. SQL is not like Excel, where you can just dump rows and columns willy-nilly.

    – Tim Biegeleisen
    Jan 4 at 6:33











  • If you want, you can create PHP code that inserts values into database and in PHP you can realize inserting value into 3rd table.

    – Hackrrr
    Jan 4 at 6:39











  • "result of the queries"? or are they the queries themselves?

    – TrebledJ
    Jan 4 at 7:32











  • @Hackrrr, basically I do not know how to concatenate the two quires into third table. Could you please suggest the piece of code. I'm showing the out put of what is writing into database. "echo $myquery;"

    – Chaitanya Husys
    Jan 4 at 9:15











  • As an aside, any time you find yourself with enumerated columns (above, say, 2) alarm bells should start ringing. Your table design is likely not optimized for the kinds of queries you'd be likely to run against it.

    – Strawberry
    Jan 4 at 9:45














  • 3





    There is no real way to do this. You need to provide one or more join columns which relate a record in the first table to one or more records in the second table. SQL is not like Excel, where you can just dump rows and columns willy-nilly.

    – Tim Biegeleisen
    Jan 4 at 6:33











  • If you want, you can create PHP code that inserts values into database and in PHP you can realize inserting value into 3rd table.

    – Hackrrr
    Jan 4 at 6:39











  • "result of the queries"? or are they the queries themselves?

    – TrebledJ
    Jan 4 at 7:32











  • @Hackrrr, basically I do not know how to concatenate the two quires into third table. Could you please suggest the piece of code. I'm showing the out put of what is writing into database. "echo $myquery;"

    – Chaitanya Husys
    Jan 4 at 9:15











  • As an aside, any time you find yourself with enumerated columns (above, say, 2) alarm bells should start ringing. Your table design is likely not optimized for the kinds of queries you'd be likely to run against it.

    – Strawberry
    Jan 4 at 9:45








3




3





There is no real way to do this. You need to provide one or more join columns which relate a record in the first table to one or more records in the second table. SQL is not like Excel, where you can just dump rows and columns willy-nilly.

– Tim Biegeleisen
Jan 4 at 6:33





There is no real way to do this. You need to provide one or more join columns which relate a record in the first table to one or more records in the second table. SQL is not like Excel, where you can just dump rows and columns willy-nilly.

– Tim Biegeleisen
Jan 4 at 6:33













If you want, you can create PHP code that inserts values into database and in PHP you can realize inserting value into 3rd table.

– Hackrrr
Jan 4 at 6:39





If you want, you can create PHP code that inserts values into database and in PHP you can realize inserting value into 3rd table.

– Hackrrr
Jan 4 at 6:39













"result of the queries"? or are they the queries themselves?

– TrebledJ
Jan 4 at 7:32





"result of the queries"? or are they the queries themselves?

– TrebledJ
Jan 4 at 7:32













@Hackrrr, basically I do not know how to concatenate the two quires into third table. Could you please suggest the piece of code. I'm showing the out put of what is writing into database. "echo $myquery;"

– Chaitanya Husys
Jan 4 at 9:15





@Hackrrr, basically I do not know how to concatenate the two quires into third table. Could you please suggest the piece of code. I'm showing the out put of what is writing into database. "echo $myquery;"

– Chaitanya Husys
Jan 4 at 9:15













As an aside, any time you find yourself with enumerated columns (above, say, 2) alarm bells should start ringing. Your table design is likely not optimized for the kinds of queries you'd be likely to run against it.

– Strawberry
Jan 4 at 9:45





As an aside, any time you find yourself with enumerated columns (above, say, 2) alarm bells should start ringing. Your table design is likely not optimized for the kinds of queries you'd be likely to run against it.

– Strawberry
Jan 4 at 9:45












1 Answer
1






active

oldest

votes


















1














I think something as this:



<?php
//First we need connect to database
$server = "localhost";
$Userdb = "admin";
$Passworddb = "password";
$database = "db";
$conn = mysqli_connect($server, $Userdb, $Passworddb, $database);
mysqli_set_charset($conn, "utf8");

//Getting values - for this example it is static
//For concate we need values as array => explode()
$colums1 = "ans1,ans2,ans3,ans4,ans5";
$values1 = explode("),(", substr("(0,0,0,0,0),(1,1,1,0,0),(0,0,0,0,0)", 1, -1));
$colums2 = "uid,cid,sid";
$values2 = explode("),(", substr("(1,abc,123),(2,def,456),(3,ghi,789)", 1, -1));

//Query for table1
$command = "INSERT INTO table1 (".$colums1.") VALUES ".$values1;
mysqli_query($conn, $command) or die(mysqli_error($conn));

//Query for table2
$command = "INSERT INTO table2 (".$colums2.") VALUES ".$values2;
mysqli_query($conn, $command) or die(mysqli_error($conn));

//We need to concate values so =>
//=> For every index of $values1 add at same index into $values3 concated $values1 and $values2

$values3 = array();
for ($x = 0; $x <= count($values1); $x++) {
$values3[$x] = "(".$values2[x].",".$values1[$x].")";
}

//Query for table3
$command = "INSERT INTO table3 (".$colums2.",".$colums1.") VALUES (".implode("),(", $values3).")";
mysqli_query($conn, $command) or die(mysqli_error($conn));
?>


I think that this SHOULD works (one never knows :) )






share|improve this answer
























  • Thank You so much @Hackrrr. You post helped me to insert the data into db table.

    – Chaitanya Husys
    Jan 7 at 8:43












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54034046%2fhow-to-concatenate-data-of-two-arrays-and-insert-into-mysql-database%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














I think something as this:



<?php
//First we need connect to database
$server = "localhost";
$Userdb = "admin";
$Passworddb = "password";
$database = "db";
$conn = mysqli_connect($server, $Userdb, $Passworddb, $database);
mysqli_set_charset($conn, "utf8");

//Getting values - for this example it is static
//For concate we need values as array => explode()
$colums1 = "ans1,ans2,ans3,ans4,ans5";
$values1 = explode("),(", substr("(0,0,0,0,0),(1,1,1,0,0),(0,0,0,0,0)", 1, -1));
$colums2 = "uid,cid,sid";
$values2 = explode("),(", substr("(1,abc,123),(2,def,456),(3,ghi,789)", 1, -1));

//Query for table1
$command = "INSERT INTO table1 (".$colums1.") VALUES ".$values1;
mysqli_query($conn, $command) or die(mysqli_error($conn));

//Query for table2
$command = "INSERT INTO table2 (".$colums2.") VALUES ".$values2;
mysqli_query($conn, $command) or die(mysqli_error($conn));

//We need to concate values so =>
//=> For every index of $values1 add at same index into $values3 concated $values1 and $values2

$values3 = array();
for ($x = 0; $x <= count($values1); $x++) {
$values3[$x] = "(".$values2[x].",".$values1[$x].")";
}

//Query for table3
$command = "INSERT INTO table3 (".$colums2.",".$colums1.") VALUES (".implode("),(", $values3).")";
mysqli_query($conn, $command) or die(mysqli_error($conn));
?>


I think that this SHOULD works (one never knows :) )






share|improve this answer
























  • Thank You so much @Hackrrr. You post helped me to insert the data into db table.

    – Chaitanya Husys
    Jan 7 at 8:43
















1














I think something as this:



<?php
//First we need connect to database
$server = "localhost";
$Userdb = "admin";
$Passworddb = "password";
$database = "db";
$conn = mysqli_connect($server, $Userdb, $Passworddb, $database);
mysqli_set_charset($conn, "utf8");

//Getting values - for this example it is static
//For concate we need values as array => explode()
$colums1 = "ans1,ans2,ans3,ans4,ans5";
$values1 = explode("),(", substr("(0,0,0,0,0),(1,1,1,0,0),(0,0,0,0,0)", 1, -1));
$colums2 = "uid,cid,sid";
$values2 = explode("),(", substr("(1,abc,123),(2,def,456),(3,ghi,789)", 1, -1));

//Query for table1
$command = "INSERT INTO table1 (".$colums1.") VALUES ".$values1;
mysqli_query($conn, $command) or die(mysqli_error($conn));

//Query for table2
$command = "INSERT INTO table2 (".$colums2.") VALUES ".$values2;
mysqli_query($conn, $command) or die(mysqli_error($conn));

//We need to concate values so =>
//=> For every index of $values1 add at same index into $values3 concated $values1 and $values2

$values3 = array();
for ($x = 0; $x <= count($values1); $x++) {
$values3[$x] = "(".$values2[x].",".$values1[$x].")";
}

//Query for table3
$command = "INSERT INTO table3 (".$colums2.",".$colums1.") VALUES (".implode("),(", $values3).")";
mysqli_query($conn, $command) or die(mysqli_error($conn));
?>


I think that this SHOULD works (one never knows :) )






share|improve this answer
























  • Thank You so much @Hackrrr. You post helped me to insert the data into db table.

    – Chaitanya Husys
    Jan 7 at 8:43














1












1








1







I think something as this:



<?php
//First we need connect to database
$server = "localhost";
$Userdb = "admin";
$Passworddb = "password";
$database = "db";
$conn = mysqli_connect($server, $Userdb, $Passworddb, $database);
mysqli_set_charset($conn, "utf8");

//Getting values - for this example it is static
//For concate we need values as array => explode()
$colums1 = "ans1,ans2,ans3,ans4,ans5";
$values1 = explode("),(", substr("(0,0,0,0,0),(1,1,1,0,0),(0,0,0,0,0)", 1, -1));
$colums2 = "uid,cid,sid";
$values2 = explode("),(", substr("(1,abc,123),(2,def,456),(3,ghi,789)", 1, -1));

//Query for table1
$command = "INSERT INTO table1 (".$colums1.") VALUES ".$values1;
mysqli_query($conn, $command) or die(mysqli_error($conn));

//Query for table2
$command = "INSERT INTO table2 (".$colums2.") VALUES ".$values2;
mysqli_query($conn, $command) or die(mysqli_error($conn));

//We need to concate values so =>
//=> For every index of $values1 add at same index into $values3 concated $values1 and $values2

$values3 = array();
for ($x = 0; $x <= count($values1); $x++) {
$values3[$x] = "(".$values2[x].",".$values1[$x].")";
}

//Query for table3
$command = "INSERT INTO table3 (".$colums2.",".$colums1.") VALUES (".implode("),(", $values3).")";
mysqli_query($conn, $command) or die(mysqli_error($conn));
?>


I think that this SHOULD works (one never knows :) )






share|improve this answer













I think something as this:



<?php
//First we need connect to database
$server = "localhost";
$Userdb = "admin";
$Passworddb = "password";
$database = "db";
$conn = mysqli_connect($server, $Userdb, $Passworddb, $database);
mysqli_set_charset($conn, "utf8");

//Getting values - for this example it is static
//For concate we need values as array => explode()
$colums1 = "ans1,ans2,ans3,ans4,ans5";
$values1 = explode("),(", substr("(0,0,0,0,0),(1,1,1,0,0),(0,0,0,0,0)", 1, -1));
$colums2 = "uid,cid,sid";
$values2 = explode("),(", substr("(1,abc,123),(2,def,456),(3,ghi,789)", 1, -1));

//Query for table1
$command = "INSERT INTO table1 (".$colums1.") VALUES ".$values1;
mysqli_query($conn, $command) or die(mysqli_error($conn));

//Query for table2
$command = "INSERT INTO table2 (".$colums2.") VALUES ".$values2;
mysqli_query($conn, $command) or die(mysqli_error($conn));

//We need to concate values so =>
//=> For every index of $values1 add at same index into $values3 concated $values1 and $values2

$values3 = array();
for ($x = 0; $x <= count($values1); $x++) {
$values3[$x] = "(".$values2[x].",".$values1[$x].")";
}

//Query for table3
$command = "INSERT INTO table3 (".$colums2.",".$colums1.") VALUES (".implode("),(", $values3).")";
mysqli_query($conn, $command) or die(mysqli_error($conn));
?>


I think that this SHOULD works (one never knows :) )







share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 4 at 11:39









HackrrrHackrrr

170212




170212













  • Thank You so much @Hackrrr. You post helped me to insert the data into db table.

    – Chaitanya Husys
    Jan 7 at 8:43



















  • Thank You so much @Hackrrr. You post helped me to insert the data into db table.

    – Chaitanya Husys
    Jan 7 at 8:43

















Thank You so much @Hackrrr. You post helped me to insert the data into db table.

– Chaitanya Husys
Jan 7 at 8:43





Thank You so much @Hackrrr. You post helped me to insert the data into db table.

– Chaitanya Husys
Jan 7 at 8:43




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54034046%2fhow-to-concatenate-data-of-two-arrays-and-insert-into-mysql-database%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas