append more than one value in single row using php - php

I have a table called user having 3 columns namely id, name and phone no.
i want insert data like below clip.
+----+---------------+---------------------+-
| id | name | phone no |
+----+---------------+---------------------+-
| 1 | mahadev | +91 XXXXX |
| 2 | swamy | +91 YYYYY |
| | | +91 ZZZZZ |
| 3 | charlie | +91 AAAAA |
| | | |
+----+---------------+---------------------+-
Here question is how can i add more than one values (one by one) to same row as showing id = 2 in above clip.
Could anyone please help me on this?
Thanks in advance.

You cannot do what you intended, how you intended. And for a reason.
One possible solution (bad), would be to make id non-unique and then insert two times id 2, name swamy, phone for two different phones.
Proper solution is to have two tables. One is your current user, which would have only id and name.
Second table is phone_numbers which would have user_id and phone_no. Primary key on that table would be composite of user_id and phone_no so it would prevent duplicates. Then in that table you can insert as many numbers as you need.
In your example you would have two rows with user_id=2, one for each phone number.
Then it is only a matter JOIN to join the two tables together and display your results.

SQL architecture don't allow such things. You need to use more than one row or you can use more than one table with foreign keys. Or you can serialize(phone no) before you put it into mysql.

One possible solution could be creating an array of that data and then storing it with serialize() function.
Small example:
$phones_array = array('phone_a' => '+91 YYYYY', 'phone_b' => '+91 ZZZZZ');
serialize($phones_array);
Now your data are serialized into a string, trying var_dump($phones_array) you should get:
string 'a:2:{s:7:"phone_a";s:9:"+91 YYYYY";s:7:"phone_b";s:9:"+91 ZZZZZ";}' (length=66)
You can now insert this value into your table
You can retrieve this data with:
unserialize($phones_array);

Related

How do I use multiple tables to create a user profile?

I made a firm to add a user to my database now I want to have two tables. One table keeps track of the languages the user knows and the other table the design software the user uses.
Would I create 3 tables (profile, languages, software) each with an I'd field and when I add a user add a row to each table?
As you begin to add several many-to-many relationships, you need more tables to 'link' the information together. Here's how I would tackle the problem:
Note The IDs should all be unique indexed columns. Consider using AUTO_INCREMENT.
Table 1: Contains user's profile information
| ProfileID |UserInfo |
|=======================|
| 0 | Info |
|-----------------------|
| 1 | Info2 |
|-----------------------|
Table 2: Stores the possible languages
|LanguageID |LanguageName|
|========================|
| 50 | Python |
|------------------------|
| 51 | Java |
|------------------------|
and so on...
Table 3: Stores the Profile links to the languages
|ProfileID |LanguageID |
|========================|
| 0 | 50 |
|------------------------|
| 0 | 51 |
|------------------------|
| 1 | 50 |
|------------------------|
Every time you wanted to add a language to a user's profile, you'd create an entry in this table.
You would add two more tables for the software a user knows. One table for all the possible types of software, and another to store the links.
When you want to retrieve the information, you would do an operation such as the one below:
SELECT * FROM Table3
LEFT JOIN Table2
ON Table3.LanguageID = Table2.LanguageID
WHERE ProfileID = [TheProfileIDToSearch]
This structure uses JOIN to link tables together to return information from several tables at once. Here is a W3Schools quick explanation about SQL JOINS.

search comma separated values in mysql using php

I am having a bit of a problem running a select query on a database. Some of the data is held as a list of comma separated values, an example:
Table: example_tbl
| Id | standardid | subjectid |
| 1 | 1,2,3 | 8,10,3 |
| 2 | 7,6,12 | 18,19,2 |
| 3 | 10,11,12 | 4,3,7 |
And an example of the kind of thing I am trying to run:
select * from table where standardid in (7,10) and subjectid in (2,3,4)
select * from table where FIND_IN_SET(7,10,standardid) and FIND_IN_SET(2,3,4,subjectid)
Thanks in advance for anything you can tell me.
comma separated values in a database are inherently problematic and inefficient, and it is far, far better to normalise your database design; but if you check the syntax for FIND_IN_SET() it looks for a single value in the set, not matches several values in the set.
To use it for multiple values, you need to use the function several times:
select * from table
where (FIND_IN_SET(7,standardid)
OR FIND_IN_SET(10,standardid))
and (FIND_IN_SET(2,subjectid)
OR FIND_IN_SET(3,subjectid)
OR FIND_IN_SET(4,subjectid))

Change existing row ID based on AUTO_INCREMENT (unique key)

I have a table that records tickets that are separated by a column that denotes the "database". I have a unique key on the database and cid columns so that it increments each database uniquely (cid has the AUTO_INCREMENT attribute to accomplish this). I increment id manually since I cannot make two AUTO_INCREMENT columns (and I'd rather the AUTO_INCREMENT take care of the more complicated task of the uniqueness).
This makes my data look like this basically:
-----------------------------
| id | cid | database |
-----------------------------
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 2 | 2 |
-----------------------------
This works perfectly well.
I am trying to make a feature that will allow a ticket to be "moved" to another database; frequently a user may enter the ticket in the wrong database. Instead of having to close the ticket and completely create a new one (copy/pasting all the data over), I'd like to make it easier for the user of course.
I want to be able to change the database and cid fields uniquely without having to tamper with the id field. I want to do an UPDATE (or the like) since there are foreign key constraints on other tables the link to the id field; this is why I don't simply do a REPLACE or DELETE then INSERT, as I don't want it to delete all of the other table data and then have to recreate it (log entries, transactions, appointments, etc.).
How can I get the next unique AUTO_INCREMENT value (based on the new database value), then use that to update the desired row?
For example, in the above dataset, I want to change the first record to go to "database #2". Whatever query I make needs to make the data change to this:
-----------------------------
| id | cid | database |
-----------------------------
| 1 | 3 | 2 |
| 2 | 1 | 2 |
| 3 | 2 | 2 |
-----------------------------
I'm not sure if the AUTO_INCREMENT needs to be incremented, as my understanding is that the unique key makes it just calculate the next appropriate value on the fly.
I actually ended up making it work once I re-read an except on using AUTO_INCREMENT on multiple columns.
For MyISAM and BDB tables you can specify AUTO_INCREMENT on a
secondary column in a multiple-column index. In this case, the
generated value for the AUTO_INCREMENT column is calculated as
MAX(auto_increment_column) + 1 WHERE prefix=given-prefix. This is
useful when you want to put data into ordered groups.
This was the clue I needed. I simply mimic'd the query MySQL runs internally according to that quote, and joined it into my UPDATE query as such. Assume $new_database is the database to move to, and $id is the current ticket id.
UPDATE `tickets` AS t1,
(
SELECT MAX(cid) + 1 AS new_cid
FROM `tickets`
WHERE database = {$new_database}
) AS t2
SET t1.cid = t2.new_cid,
t1.database = {$new_database}
WHERE t1.id = {$id}

Reorder/reset column unique id in mysql

I do not know a lot of mysql. I have two tables on same server:
A.artists
id | artist_name
--------------------
8 | XXXX |
1 | YYYY |
5 | ZZZZ |
A.albums
id | artist_id | album_name
-----------------------------
1 | 5 | Album 1
2 | 1 | Album 2
3 | 8 | Album 3
I want to reorder the artist_id column, accordingly, at the same time I want to change A.artists's id column from reordered artist_id id.
Is it possible such a thing? How can I do that? Thanks!
First, you need to understand that tables in SQL are unordered. So, you cannot specify an ordering for the table. You can specify an ordering for a query.
My guess is that you want to join the two tables to get the name with the albums:
select al.*, ar.name
from albums al join
artists ar
on al.artist_id = ar.id
order by ar.name
The way your question is phrased is that you want to re-assign ids. Normally, this would not be necessary, since the ids exist only to uniquely identify rows. If you have a good reason for reassigning ids, then modify your question or ask a new question, including the reason.

comparing values between different rows of database and getting maximum count

i have a table in which a row contains following data. So i need to compare data among themselves and show which data has maximum count.for ex. my table has following fruits name. So i need to compare these fruits among themselves and show max fruit count first.
s.no | field1 |
1 |apple,orange,pineapple |
2 |apple,pineapple,strawberry,grapes|
3 |apple,grapes, |
4 |orange,mango |
i.e apple comes first,grapes second,pineapple third and so on. and these datas are entered dynamically, so whatever the values is entered dynamically it needs to compare among themselves and get max count
Great question.
This is a classical bad outcome of not having the data normalized.
I recommend you to read about Database Normalization, normalize your tables and see after that how easy it is to do this with simple SQL queries
If you need to run queries on column field 1, then why not consider normalization ? Otherwise it might keep on getting complex and dirty in future.
Your current table will look like this (for serianl number 1 only), Pk can be an autoincrement primary key.
Pk | s.no |fruitId|
1 | 1 |1 |
2 | 1 |2 |
3 | 1 |3 |
Your New Table of Fruits
PK |fruitName |
1 |Apple |
2 |Orange |
3 |Pineapple |
This also helps you to avoid redundancy.
Quick solution would be counting the amount of fruits where you insert/update the row and add a fruitCount column. You can then use this column to order by.
Zohaib has to correct solution though - if you have the time and possibility for such changes. And I definitely suggest you to read Tudor's link!

Categories