0

Is it possible to save multiple entries in one column only?

I have an array and i save like this

foreach($studentlist as $value) {
    $stmt7 = $dbh - > prepare("INSERT INTO student_tbl (student_id,student_name) VALUES (?,?)");
    $stmt7 - > bindValue(1, $value['studentid'], PDO::PARAM_STR);
    $stmt7 - > bindValue(2, $value['studentname'], PDO::PARAM_STR);
    $stmt7 - > execute();
}

this will result in number of rows depending on the number of students. How can i make it in way that all the $value['studentname'] will be saved in one column in one row. It can be separated by coma. Is is possible to save array like in data base

Brownman Revival
  • 3,620
  • 9
  • 31
  • 69
  • you want to insert data like 1,2,3,4? – Deenadhayalan Manoharan Mar 07 '15 at 10:38
  • yes just like that i cant figure out how to save it like that. i dont even know if it is possible, if there are bad implications in doing so – Brownman Revival Mar 07 '15 at 10:38
  • @HogRider Don't do that! Keep your [DB normalized!](http://en.wikipedia.org/wiki/Database_normalization) *if there are bad implications* Yes there are! It will be very hard to do querys with your db – Rizier123 Mar 07 '15 at 10:39
  • @Rizier123 just as i thought there is a bad implication right?arg then i have to stick the current one .can i ask what are the negative result if i do it like that? – Brownman Revival Mar 07 '15 at 10:40
  • Are you looking for to replace INSERT INTO student_tbl (student_id,student_name) VALUES (?,?) WITH INSERT INTO student_tbl (student_id,student_name) VALUES (array of studentid, studentname}) ? – bestprogrammerintheworld Mar 07 '15 at 10:42
  • 2
    @HogRider 1. You will have a very hard time to do querys and find stuff in your db 2. You will have redundant data and you will end up with just a mess of a DB. I would recommend you to normalize your db to the 3d NF (4,5 and 6 are a bit of an overkill) – Rizier123 Mar 07 '15 at 10:42
  • @Rizier123 you have my gratitude sir i am glad i decided to clear this out first before doing it in my project. – Brownman Revival Mar 07 '15 at 10:43
  • 3
    See also [Is storing a delimited list in a database column really that bad?](http://stackoverflow.com/q/3653462). – eggyal Mar 07 '15 at 10:45
  • @eggyal really helpful i cant find the exact key word to search that is why i didnt end up on the link but i am glad i was able to read it. reading the first line about data type is one important thing for me you have my gratitude as well – Brownman Revival Mar 07 '15 at 10:48
  • I think you can use json_encode.. – Kedar B Mar 07 '15 at 12:00

0 Answers0