-1

I am working with mysql, I have table "wp_notification" and here is currently strcture of table

id              UserId                PostId              status
1               s:1:"8";              26                  accept          

In "UserId" column, i am saving Userid of users in "array" format using "serialize" function, but now i want to update( add one more user id in this column) this column So how can i do this ? Is this possible with mysql query only ?

Amy
  • 186
  • 8
  • Why is it not `i:1:8`? Also, you can just run an update query on just that column. – geertjanknapen May 25 '22 at 06:27
  • @geertjanknapen: i have userId and with use of "serialize" function i saved into database now want to add "one or multiple" data/UserId in this column – Amy May 25 '22 at 06:31
  • Well `userId` should be an `array`, I guess. Unserialize userId, `array_push` the new userId, serialize array again, update in database. – geertjanknapen May 25 '22 at 06:39
  • @geertjanknapen:means new record should be like s:1:"8";, s:1:"9"; am i right ? – Amy May 25 '22 at 06:43
  • 1
    Why not just insert the userId 8 into that column? Why serialize the data? – brombeer May 25 '22 at 06:56
  • _"new record should be like s:1:"8";, s:1:"9"; am i right "_ - no, because that is not a valid serialize value to begin with. The info, that this is supposed to be an _array_ of string values, is completely missing here. – CBroe May 25 '22 at 06:57
  • @CBroe: then 1) which is valid serialize ? 2) should i use serialize ? 3) if no then in which format/style should be use ? – Amy May 25 '22 at 07:01
  • 1) You can easily figure that out yourself, by _using_ `serialize` on such an array. 2) No. 3) Keyword is proper _normalization_. – CBroe May 25 '22 at 07:02
  • @Amy as I said before, you need to serialize an array if you want to provide multiple userIds there. But you really should wonder why you would want to do this instead of just inserting 8 – geertjanknapen May 25 '22 at 07:03
  • @geertjanknapen because there can be multiple id's ( 8 , 9 , 7 ...) , what should i do then ? – Amy May 25 '22 at 07:07
  • 1
    row:`| id: 1 | userId: 8 | postId: 26| status: accept |` Another row: `| id: 2 | userId: 9 | postId: 26 | status: accept |` @Amy – geertjanknapen May 25 '22 at 07:10
  • @geertjanknapen understand , Thank you for the help – Amy May 25 '22 at 07:21

1 Answers1

0

You don't need to update the column. You can add one more user id in the future/current cells in that column.

It is worth noting however that using a "serialized" output string as an Id in a database is not a good solution. See Normalization in MYSQL .

Akira Taguchi
  • 111
  • 1
  • 7
  • i want to insert "multiple userId" in column so what should i do ? – Amy May 25 '22 at 06:32
  • This is a good question. The answer is: You shouldn't do this in one table. You need to learn about 1TM relationships in databases and then implement it using multiple tables: [1TM relationships](https://medium.com/@emekadc/how-to-implement-one-to-one-one-to-many-and-many-to-many-relationships-when-designing-a-database-9da2de684710) – Akira Taguchi May 25 '22 at 06:47
  • if i avoid serialize then can i save multipel id in one column ? if yes then how ? – Amy May 25 '22 at 07:02
  • 1
    @Amy You do not want to save multiple ids in one column. Add a new row for every user id you want to save – brombeer May 25 '22 at 07:04
  • @brombeer: can i do this without new row ? can i save in single column ? if yes then how ? – Amy May 25 '22 at 07:08
  • @Amy You can do this without a new row. However you should not do this in one row. – Akira Taguchi May 25 '22 at 07:12
  • 1
    @Amy Multiple people advised to _not_ store multiple ids in one column. Why would you want to do this? This kind of defeats the purpose of a relational database. If you really really want to do this (for reasons only you know) you create an array, serialize it and store it in that column. – brombeer May 25 '22 at 07:17
  • @brombeer okay understand – Amy May 25 '22 at 07:22