Php array to column row in mysql table










0















I have array named olmali



$olmali = $_POST['result'];


and print_r($olmali);
Result is below :



Array ( 
[0] => 1
[1] => 1
[2] => 20
[3] => 2
[4] => 3
[5] => 5
[6] => 6
[7] => 7
[8] => 9
[9] => 8
[10] => 10
[11] => 11
[12] => 13
[13] => 12
[14] => 12
[15] => 14
[16] => 15
[17] => 16
[18] => 17
[19] => 17
[20] => 19
[21] => 20
)


Result are going to test column in SQL table in phpmyadmin like :



id test
1 array


But I expect :



id test
1 1
2 1
3 20
4 2
5 3
6 ....and goes on


How can I resolve this problem ? İs there any way and how can I do it. PHP array to column row in MySQL table like that










share|improve this question



















  • 3





    Result are going to test column in SQL table - where is your code?

    – splash58
    Nov 14 '18 at 9:05












  • Do further reading here stackoverflow.com/questions/10054633/…

    – Kimigx
    Nov 14 '18 at 9:26















0















I have array named olmali



$olmali = $_POST['result'];


and print_r($olmali);
Result is below :



Array ( 
[0] => 1
[1] => 1
[2] => 20
[3] => 2
[4] => 3
[5] => 5
[6] => 6
[7] => 7
[8] => 9
[9] => 8
[10] => 10
[11] => 11
[12] => 13
[13] => 12
[14] => 12
[15] => 14
[16] => 15
[17] => 16
[18] => 17
[19] => 17
[20] => 19
[21] => 20
)


Result are going to test column in SQL table in phpmyadmin like :



id test
1 array


But I expect :



id test
1 1
2 1
3 20
4 2
5 3
6 ....and goes on


How can I resolve this problem ? İs there any way and how can I do it. PHP array to column row in MySQL table like that










share|improve this question



















  • 3





    Result are going to test column in SQL table - where is your code?

    – splash58
    Nov 14 '18 at 9:05












  • Do further reading here stackoverflow.com/questions/10054633/…

    – Kimigx
    Nov 14 '18 at 9:26













0












0








0








I have array named olmali



$olmali = $_POST['result'];


and print_r($olmali);
Result is below :



Array ( 
[0] => 1
[1] => 1
[2] => 20
[3] => 2
[4] => 3
[5] => 5
[6] => 6
[7] => 7
[8] => 9
[9] => 8
[10] => 10
[11] => 11
[12] => 13
[13] => 12
[14] => 12
[15] => 14
[16] => 15
[17] => 16
[18] => 17
[19] => 17
[20] => 19
[21] => 20
)


Result are going to test column in SQL table in phpmyadmin like :



id test
1 array


But I expect :



id test
1 1
2 1
3 20
4 2
5 3
6 ....and goes on


How can I resolve this problem ? İs there any way and how can I do it. PHP array to column row in MySQL table like that










share|improve this question
















I have array named olmali



$olmali = $_POST['result'];


and print_r($olmali);
Result is below :



Array ( 
[0] => 1
[1] => 1
[2] => 20
[3] => 2
[4] => 3
[5] => 5
[6] => 6
[7] => 7
[8] => 9
[9] => 8
[10] => 10
[11] => 11
[12] => 13
[13] => 12
[14] => 12
[15] => 14
[16] => 15
[17] => 16
[18] => 17
[19] => 17
[20] => 19
[21] => 20
)


Result are going to test column in SQL table in phpmyadmin like :



id test
1 array


But I expect :



id test
1 1
2 1
3 20
4 2
5 3
6 ....and goes on


How can I resolve this problem ? İs there any way and how can I do it. PHP array to column row in MySQL table like that







php mysql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 9:05









executable

1,8902823




1,8902823










asked Nov 14 '18 at 8:58









askermanaskerman

46




46







  • 3





    Result are going to test column in SQL table - where is your code?

    – splash58
    Nov 14 '18 at 9:05












  • Do further reading here stackoverflow.com/questions/10054633/…

    – Kimigx
    Nov 14 '18 at 9:26












  • 3





    Result are going to test column in SQL table - where is your code?

    – splash58
    Nov 14 '18 at 9:05












  • Do further reading here stackoverflow.com/questions/10054633/…

    – Kimigx
    Nov 14 '18 at 9:26







3




3





Result are going to test column in SQL table - where is your code?

– splash58
Nov 14 '18 at 9:05






Result are going to test column in SQL table - where is your code?

– splash58
Nov 14 '18 at 9:05














Do further reading here stackoverflow.com/questions/10054633/…

– Kimigx
Nov 14 '18 at 9:26





Do further reading here stackoverflow.com/questions/10054633/…

– Kimigx
Nov 14 '18 at 9:26












2 Answers
2






active

oldest

votes


















1














You have to loop through the array and insert one by one.



foreach($olmali as $v)

//insert query goes here
$sql = "INSERT INTO tbl_name (test) VALUES ('$v')";

// Then Execute the query



Another approach is to use Bulk Insert,



$sql = "INSERT INTO tbl_name (test) VALUES "; 

foreach($olmali as $v)

//Concatenate values in bulk
$sql .= "('$v'),";


$sql = rtrim($sql, ','); // Remove extra comma at the end
// Then execute query





share|improve this answer

























  • Concatting the query like @suresh is doing is more cost efficient

    – Gobbin
    Nov 14 '18 at 9:08











  • Yes, it can be done using bulk insert. Making the OP understand basics first :). Added bulk insert example in my answer too.

    – Samir
    Nov 14 '18 at 9:09












  • Nice. Good answer!

    – Gobbin
    Nov 14 '18 at 9:14











  • thanks very much for your solution . Bulk insert is working perfect

    – askerman
    Nov 14 '18 at 14:24











  • how can ı do this with sql update command?

    – askerman
    Nov 14 '18 at 19:49


















0














there are many ways
1:using Bulk insert



$qry = "INSERT INTO tableName(test) VALUES ";
foreach($olmali as $val)
$qry .="($val),";


$qry = preg_replace("/,$/", ";", $qry);

$conn->query($sql);


2:using prepare statement preferred way



$stmt = $db->stmt_init();
$stmt->prepare("INSERT INTO tableName(test) VALUES(?)");
foreach($olmali as $val)

$stmt->bind_param('i',$val);
$stmt->execute();

$stmt->close();





share|improve this answer

























  • thank you very much Suresh .It works....

    – askerman
    Nov 14 '18 at 14:21











  • how can ı do this with sql update command ? not insert command

    – askerman
    Nov 14 '18 at 21:00











  • $stmt = $db->stmt_init(); $stmt->prepare("UPDATE tableName SET test=? WHERE id=?"); foreach($olmali as $key=>$val) $stmt->bind_param('ii',$val,$key); $stmt->execute();

    – suresh bambhaniya
    Nov 15 '18 at 4:12











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%2f53296310%2fphp-array-to-column-row-in-mysql-table%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You have to loop through the array and insert one by one.



foreach($olmali as $v)

//insert query goes here
$sql = "INSERT INTO tbl_name (test) VALUES ('$v')";

// Then Execute the query



Another approach is to use Bulk Insert,



$sql = "INSERT INTO tbl_name (test) VALUES "; 

foreach($olmali as $v)

//Concatenate values in bulk
$sql .= "('$v'),";


$sql = rtrim($sql, ','); // Remove extra comma at the end
// Then execute query





share|improve this answer

























  • Concatting the query like @suresh is doing is more cost efficient

    – Gobbin
    Nov 14 '18 at 9:08











  • Yes, it can be done using bulk insert. Making the OP understand basics first :). Added bulk insert example in my answer too.

    – Samir
    Nov 14 '18 at 9:09












  • Nice. Good answer!

    – Gobbin
    Nov 14 '18 at 9:14











  • thanks very much for your solution . Bulk insert is working perfect

    – askerman
    Nov 14 '18 at 14:24











  • how can ı do this with sql update command?

    – askerman
    Nov 14 '18 at 19:49















1














You have to loop through the array and insert one by one.



foreach($olmali as $v)

//insert query goes here
$sql = "INSERT INTO tbl_name (test) VALUES ('$v')";

// Then Execute the query



Another approach is to use Bulk Insert,



$sql = "INSERT INTO tbl_name (test) VALUES "; 

foreach($olmali as $v)

//Concatenate values in bulk
$sql .= "('$v'),";


$sql = rtrim($sql, ','); // Remove extra comma at the end
// Then execute query





share|improve this answer

























  • Concatting the query like @suresh is doing is more cost efficient

    – Gobbin
    Nov 14 '18 at 9:08











  • Yes, it can be done using bulk insert. Making the OP understand basics first :). Added bulk insert example in my answer too.

    – Samir
    Nov 14 '18 at 9:09












  • Nice. Good answer!

    – Gobbin
    Nov 14 '18 at 9:14











  • thanks very much for your solution . Bulk insert is working perfect

    – askerman
    Nov 14 '18 at 14:24











  • how can ı do this with sql update command?

    – askerman
    Nov 14 '18 at 19:49













1












1








1







You have to loop through the array and insert one by one.



foreach($olmali as $v)

//insert query goes here
$sql = "INSERT INTO tbl_name (test) VALUES ('$v')";

// Then Execute the query



Another approach is to use Bulk Insert,



$sql = "INSERT INTO tbl_name (test) VALUES "; 

foreach($olmali as $v)

//Concatenate values in bulk
$sql .= "('$v'),";


$sql = rtrim($sql, ','); // Remove extra comma at the end
// Then execute query





share|improve this answer















You have to loop through the array and insert one by one.



foreach($olmali as $v)

//insert query goes here
$sql = "INSERT INTO tbl_name (test) VALUES ('$v')";

// Then Execute the query



Another approach is to use Bulk Insert,



$sql = "INSERT INTO tbl_name (test) VALUES "; 

foreach($olmali as $v)

//Concatenate values in bulk
$sql .= "('$v'),";


$sql = rtrim($sql, ','); // Remove extra comma at the end
// Then execute query






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 14 '18 at 9:13

























answered Nov 14 '18 at 9:06









SamirSamir

5,3492628




5,3492628












  • Concatting the query like @suresh is doing is more cost efficient

    – Gobbin
    Nov 14 '18 at 9:08











  • Yes, it can be done using bulk insert. Making the OP understand basics first :). Added bulk insert example in my answer too.

    – Samir
    Nov 14 '18 at 9:09












  • Nice. Good answer!

    – Gobbin
    Nov 14 '18 at 9:14











  • thanks very much for your solution . Bulk insert is working perfect

    – askerman
    Nov 14 '18 at 14:24











  • how can ı do this with sql update command?

    – askerman
    Nov 14 '18 at 19:49

















  • Concatting the query like @suresh is doing is more cost efficient

    – Gobbin
    Nov 14 '18 at 9:08











  • Yes, it can be done using bulk insert. Making the OP understand basics first :). Added bulk insert example in my answer too.

    – Samir
    Nov 14 '18 at 9:09












  • Nice. Good answer!

    – Gobbin
    Nov 14 '18 at 9:14











  • thanks very much for your solution . Bulk insert is working perfect

    – askerman
    Nov 14 '18 at 14:24











  • how can ı do this with sql update command?

    – askerman
    Nov 14 '18 at 19:49
















Concatting the query like @suresh is doing is more cost efficient

– Gobbin
Nov 14 '18 at 9:08





Concatting the query like @suresh is doing is more cost efficient

– Gobbin
Nov 14 '18 at 9:08













Yes, it can be done using bulk insert. Making the OP understand basics first :). Added bulk insert example in my answer too.

– Samir
Nov 14 '18 at 9:09






Yes, it can be done using bulk insert. Making the OP understand basics first :). Added bulk insert example in my answer too.

– Samir
Nov 14 '18 at 9:09














Nice. Good answer!

– Gobbin
Nov 14 '18 at 9:14





Nice. Good answer!

– Gobbin
Nov 14 '18 at 9:14













thanks very much for your solution . Bulk insert is working perfect

– askerman
Nov 14 '18 at 14:24





thanks very much for your solution . Bulk insert is working perfect

– askerman
Nov 14 '18 at 14:24













how can ı do this with sql update command?

– askerman
Nov 14 '18 at 19:49





how can ı do this with sql update command?

– askerman
Nov 14 '18 at 19:49













0














there are many ways
1:using Bulk insert



$qry = "INSERT INTO tableName(test) VALUES ";
foreach($olmali as $val)
$qry .="($val),";


$qry = preg_replace("/,$/", ";", $qry);

$conn->query($sql);


2:using prepare statement preferred way



$stmt = $db->stmt_init();
$stmt->prepare("INSERT INTO tableName(test) VALUES(?)");
foreach($olmali as $val)

$stmt->bind_param('i',$val);
$stmt->execute();

$stmt->close();





share|improve this answer

























  • thank you very much Suresh .It works....

    – askerman
    Nov 14 '18 at 14:21











  • how can ı do this with sql update command ? not insert command

    – askerman
    Nov 14 '18 at 21:00











  • $stmt = $db->stmt_init(); $stmt->prepare("UPDATE tableName SET test=? WHERE id=?"); foreach($olmali as $key=>$val) $stmt->bind_param('ii',$val,$key); $stmt->execute();

    – suresh bambhaniya
    Nov 15 '18 at 4:12
















0














there are many ways
1:using Bulk insert



$qry = "INSERT INTO tableName(test) VALUES ";
foreach($olmali as $val)
$qry .="($val),";


$qry = preg_replace("/,$/", ";", $qry);

$conn->query($sql);


2:using prepare statement preferred way



$stmt = $db->stmt_init();
$stmt->prepare("INSERT INTO tableName(test) VALUES(?)");
foreach($olmali as $val)

$stmt->bind_param('i',$val);
$stmt->execute();

$stmt->close();





share|improve this answer

























  • thank you very much Suresh .It works....

    – askerman
    Nov 14 '18 at 14:21











  • how can ı do this with sql update command ? not insert command

    – askerman
    Nov 14 '18 at 21:00











  • $stmt = $db->stmt_init(); $stmt->prepare("UPDATE tableName SET test=? WHERE id=?"); foreach($olmali as $key=>$val) $stmt->bind_param('ii',$val,$key); $stmt->execute();

    – suresh bambhaniya
    Nov 15 '18 at 4:12














0












0








0







there are many ways
1:using Bulk insert



$qry = "INSERT INTO tableName(test) VALUES ";
foreach($olmali as $val)
$qry .="($val),";


$qry = preg_replace("/,$/", ";", $qry);

$conn->query($sql);


2:using prepare statement preferred way



$stmt = $db->stmt_init();
$stmt->prepare("INSERT INTO tableName(test) VALUES(?)");
foreach($olmali as $val)

$stmt->bind_param('i',$val);
$stmt->execute();

$stmt->close();





share|improve this answer















there are many ways
1:using Bulk insert



$qry = "INSERT INTO tableName(test) VALUES ";
foreach($olmali as $val)
$qry .="($val),";


$qry = preg_replace("/,$/", ";", $qry);

$conn->query($sql);


2:using prepare statement preferred way



$stmt = $db->stmt_init();
$stmt->prepare("INSERT INTO tableName(test) VALUES(?)");
foreach($olmali as $val)

$stmt->bind_param('i',$val);
$stmt->execute();

$stmt->close();






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 15 '18 at 4:08

























answered Nov 14 '18 at 9:06









suresh bambhaniyasuresh bambhaniya

945315




945315












  • thank you very much Suresh .It works....

    – askerman
    Nov 14 '18 at 14:21











  • how can ı do this with sql update command ? not insert command

    – askerman
    Nov 14 '18 at 21:00











  • $stmt = $db->stmt_init(); $stmt->prepare("UPDATE tableName SET test=? WHERE id=?"); foreach($olmali as $key=>$val) $stmt->bind_param('ii',$val,$key); $stmt->execute();

    – suresh bambhaniya
    Nov 15 '18 at 4:12


















  • thank you very much Suresh .It works....

    – askerman
    Nov 14 '18 at 14:21











  • how can ı do this with sql update command ? not insert command

    – askerman
    Nov 14 '18 at 21:00











  • $stmt = $db->stmt_init(); $stmt->prepare("UPDATE tableName SET test=? WHERE id=?"); foreach($olmali as $key=>$val) $stmt->bind_param('ii',$val,$key); $stmt->execute();

    – suresh bambhaniya
    Nov 15 '18 at 4:12

















thank you very much Suresh .It works....

– askerman
Nov 14 '18 at 14:21





thank you very much Suresh .It works....

– askerman
Nov 14 '18 at 14:21













how can ı do this with sql update command ? not insert command

– askerman
Nov 14 '18 at 21:00





how can ı do this with sql update command ? not insert command

– askerman
Nov 14 '18 at 21:00













$stmt = $db->stmt_init(); $stmt->prepare("UPDATE tableName SET test=? WHERE id=?"); foreach($olmali as $key=>$val) $stmt->bind_param('ii',$val,$key); $stmt->execute();

– suresh bambhaniya
Nov 15 '18 at 4:12






$stmt = $db->stmt_init(); $stmt->prepare("UPDATE tableName SET test=? WHERE id=?"); foreach($olmali as $key=>$val) $stmt->bind_param('ii',$val,$key); $stmt->execute();

– suresh bambhaniya
Nov 15 '18 at 4:12


















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%2f53296310%2fphp-array-to-column-row-in-mysql-table%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







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3

Museum of Modern and Contemporary Art of Trento and Rovereto