MySQL PHP Select and Edit Values From Database [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I have a database containing a list of characters for a game. The user is required to search for 5 characters and assign a position to each selected character. This list of 5 with positions needs to be saved so that it can be retrieved later.
In VB.NET I simply created a file that contained the selected 5 with their assigned positions and read the data from this.
Now I am trying to create a web-based version I am struggling as to how I should save the data. The large database is in a MySQL table. I am guessing that it wouldn't make sense to create a new table for each user and have individual rows for their selected characters? My thinking is to have a single table called 'users' then store the selected characters in a column with their position e.g. "player1:pos4,player56:pos302,player322:pos392' etc. then I can split the values later.
Does anyone have any more efficient ideas on how to approach this? I am very new to all this thanks!

I would create 3 basic tables: users, characters, positions, and 1 lookup table: usercharpos with 3 columns: user_id, char_id, pos_id. Each user, character and position should have a unique id as primary key. Then, in the usercharpos lookup table I would associate a user with the 5 chosen characters and the position of each one. For example, we have a user with id = 5, that chose some characters with ids = 1,2,3,4,5 and chose some positions for them with ids = 2,3,6,7,8. The usercharpos would become:
user_id char_id pos_id
5 1 2
5 2 3
5 3 6
5 4 7
5 5 8
That way, you keep your database normalized and each entity has its own table without multiple values in a field.

Related

display result from 2 table [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am working for small project, the main idea of the project to display
some tours:
DB - Database consist of 2 table like below
The first table consist of 4 raw:
id , tour_id , tour_name, image
and the second table for each tour details like destination and places
and raw is:
id , tour_id , from , to
The main page is done, I can display information of tour with images and url link to details page like www.domain.com/tours/get_tour?=1
where 1 is the tour_id.
My question is how can I show the information from the second table with respect tour_id?
I am working in PHP and HTML and MYSQL.
You need JOIN function of mysql. The use this type of SQL script with PHP:
$sql="SELECT first.tour_id, first.tour_image, second.tour_id
FROM first
JOIN second ON first.tour_id=second.tour_id;";
$results=$conn-->$sql; //with $conn your connection variable
$get_tour = $result->fetch_assoc();
In this way you can get first.tour_name and second.from/to depending on tour_id
Then run a while to export your datas in PHP.
$get_tour is now populated. To call elements, use
$get_tour['first.tour_id'];
$get_tour['second.from'];
...
Let me know if it works!

how print unique values in 2 columns using php [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
two columns
user_id purchase_item
1 watch
1 TV
2 watch
2 fridge
2 Toys
when i use distinct comand in sql
same as it is show data but i want to following
user_id:1
purchase_item:watch
tv
user_id:2
purchase_item:watch
fridge
Toys
means i want user id print only one time so how it is possible in php
plz help me and tell coding please
If you want to do it completely in Mysql you can use aggregation to get items as comma separated list per user
select user_id, group_concat(purchase_item) purchase_item
from table
group by user_id
this will give you results like
user_id purchase_item
1 watch,TV
2 watch,fridge,Toys
Also note using group_concat the result is truncated to the maximum length that is given by the group_concat_max_len system variable, which has a default value of 1024. The value can be set higher, although the effective maximum length of the return value is constrained by the value of max_allowed_packet
But its better if you do this in php just get the ordered results and apply your logic to show user_id only once for multiple purchase_item related to that user
select * from table order by user_id asc

Random like order with mysql and PHP [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I use PHP and Mysql. I have a table that looks kind of like this:
id title
----------
1 my title
2 another title
3 The last title
Now I want to select them with a random like order.
I will need to use LIMIT because of the query size.
The random like order should always be the same random order every time.
Example result, every time
3 The last title
1 my title
2 another title
Do another query run:
3 The last title
1 my title
2 another title
The same random like result appear.
Possible solutions
Add a real random number stored as a new column generated by insert.
Some fancy SELECT query that does some magic.
Something else?
Why I want this is that I insert products, first from one site, then from another. In the result I want to present them as a mix.
It's not really random at all, but then again your request wouldn't work with random numbers. You want some sort of a hash of each record to use in the order by. You can probably find something better, but as a simple example you could use:
SELECT * FROM my_table ORDER BY MOD(id, 2);
Which you can see working here:
http://sqlfiddle.com/#!9/269a4/1/0
Use ORDER BY RAND().So your query should be :
select * from <tablename> where <cond> then ORDER BY RAND() limit <startlimit>, <end limit>

How can I Select duplicate results, insert the results to a new table and delete them from the original table? (phpmyadmin) [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have 2 tables, both with the same column names that look like this
I have an app that is used for clocking in/out. A user goes up to a tablet and selects there name, the data is then inputted into table 'clocktable' as shown above.
What I need to happen is when a user presses their name for a second time (to clock out) it detects that there is already a value in the table with the same employeename value, and moves both clocks to a different table 'oldclocks' so that a record is kept of clocking in/out times. The reason it needs to be in a separate table is I have a web page that displays the 'clocktable' table so we can determine who is in the building.
How can I go about doing this?
I can execute
SELECT DISTINCT EmployeeName, time FROM clocktable ORDER BY EmployeeName
from within myphp and then execute
INSERT INTO oldclocks SELECT Employeename, Department, time FROM clocktable;
Afterwards, but I cannot seem to execute them both at the same time in order to get them into a php script. Also is there a way I can delete the select results from the 'clocktable' at the same time?
Help with this would be greatly appreciated.
You can add an indicator, where 1 is in and 0 is out & if you want to save the logs, then creat another table to INSERT a record whenever the indicator's value change.
1- add 2 new attributes in table A, lets say userid & in_out for an example.
2- in_out = 1/0 where 1 is in and 0 is out
3- creat table B and insert userid & time every time the in_out value change.
And that's how you know if the user in the building or out, also you will keep logs of every move
2 tables are not nessesary. And it is bad idea.
Why dont you simply add boolean IN_BUILDING which will have 0 ->Not in building and 1->In building. Then you can do:
SELECT EmployeeName FROM clocktable WHERE IN_BUILDING = 0; which will show you all employees who are not in building
or
SELECT EmployeeName FROM clocktable WHERE IN_BUILDING = 1; which will show you all employees who are in building that moment.
Its easier also because you can change it value easy on click.

"likes for comments" database designing? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
i have database tables for Posts and comments.
i want to allow users to put a like or dislike for each comments and posts.
so..i have few ideas of doing this. please tell me either i am correct or not.
create two additional columns in comments table.
likes | liked_uids
if a person clicks on like button then add +1 for the current value in likes field, else add -1 for current value.
and add user's id to liked_uids field as a sting separated by "-" dashes.
then next time i can get that string to an array and check that,
either current user id has recorded or not. if user id is, then can decide that user have participated for liking.
but i have little problem on this structure, that what will if more than one user going to like at once for a post ? then i may lose some data from liked_uids string (one last uid).
so please tell me what is the correct way of doing this?
You can create like this->
id type ('comment/like') uid comment post_id
1 comment 1 good post 100
2 like 2 null 101
3 like 1 null 102
4 comment 3 bad post 104
It is not recommended to store like count.If you want to count the likes for a particular post:
select count(*) from tableName where post_id = 100
Storing user id separted by any delimiter will land you on problems, Hence not recommended. It will be tidies job to update or retrieve if your store user id using delimiter.
If you want to see if particular user is liked a particular post or not, use below query:
select count(*) from tableName where post_id = 100 AND uid =1
One way is to use a separate Likes table with columns Likes, DisLikes, Likes_UID, DisLikes_UID and mapping table for comments and likes ex: Comments_Likes and posts and likes Posts_Likes
I'm typing way too slow on my mobile ^^ Everything already answered.
I never did anything that is similar, but I wouldn't add the two columns in the comment table. I would rather create a new table like "votes" and it would have following columns.
comment_ref | like | user_ref
Every time someone likes a comment you insert a new line there. You could also make the combination comment_ref and user_ref as a key, so you can't insert it twice.
In the end you would just make a query as such to get the votes of a single comment.
SELECT COUNT(*) FROM votes WHERE comment_ref = 123

Categories