0

Example:

$words = 'word1,word2,word3';

Ouput:

id    value1  value2 value3
---   ------  ------ -------
1     word1   word2  word3

I want the data to be inserted in a single row but in different columns

This is what I have tried so far:

$data = '"1","1","8009","8989"';
$stmt = odbc_prepare($con," INSERT INTO stock( SiteId, DatabaseId, Code, Category) VALUES(?,?,?,?)");
$success = odbc_execute($stmt,array($data) );
odbc_close($con);
8
  • 1
    What have you tried so far? :-) Commented Feb 12, 2018 at 15:05
  • Your title says MySQL, and your tag says SQL Server 2008. Which is it? Commented Feb 12, 2018 at 15:05
  • So explode $words on the comma to get an array of words; then you can insert them as individual words.... although why this table structure? Commented Feb 12, 2018 at 15:07
  • edit your post stackoverflow.com/posts/48749373/edit not including code in comments Commented Feb 12, 2018 at 15:13
  • Will there only ever be 3 words? Could there be 1? Perhaps 4, 10, more? Commented Feb 12, 2018 at 15:15

1 Answer 1

1

use explode function

<?php
$words = 'word1,word2,word3';

$data_array = explode(',', $words);

    $a=$data_array[0];
    $b=$data_array[1];
    $c=$data_array[2];
    //insert Qquery here.
?>
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you ,one challenge is i have tables with +300 columns is possible to use $data_array in my insert Query without assigning each $data_array element to a variable
If you have tables with 300+ columns like this, then you have a database design problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.