Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
how to copy table between two database using php+mysql?
sometimes I want to use php copy table derectly between two tables, it seems too hard for me. is there anyone know how to do it ?pls see my code bellow:
<?php
// database: a , table: user
name | age | sex
jimmy | 30 | male
ricky | 20 | male
tina | 20 | female
// database: b , table: user
name | age
kelvin | 30
// I want to copy a.user to replace b.user completely, how to do it in php+mysql?
$db_a = $db->connect($a_config);
$db_b = $db->connect($b_config);
// $db_a->table('user')->copy_to($db_b); // only explaining what I want to do
// final result: b.user is
name | age | sex
jimmy | 30 | male
ricky | 20 | male
tina | 20 | female
create table db_a.table_name like db_b.table_name;
After creating table you have to insert data like this
insert into db_b.table_name select * from db_a.table_name;
Related
I have a question table organised as shown bellow:
ID | question | a | b | c | d | e | correct_item
1 | What's my name? | joao | pedro | jose | mateus | lucas | b
2 | How old am I? | 15 | 18 | 20 | 22 | 25 | d
3 | Where are you from? | Paris| Berlin| Tokyo| Nairobi| Rio | d
When the user access his profile page, there is a modal button that selects one random question from the database.
$busca_questoes = "SELECT * FROM questoes ORDER BY rand() LIMIT 1";
$result_questoes = mysqli_query($conexao, $busca_questoes);
$questoes = mysqli_fetch_array($result_questoes);
What i want to know is:
How to sort questions randomly, but, before showing the question to the user, I want to check if the user has already answered correctly this question and, if so, to select a new one;
First, I tried to build a second table to integrate users database with questions database. Code bellow:
id_table | id_question | id_user | correct
1 | 1 | 2 | yes
2 | 2 | 2 | no
3 | 1 | 1 | yes
But the problem that I find with this strategy is that it is necessary to correlate each question with the user almost manually. I could do this when the user sign up into the website, but then what should I do when new questions are added?
Now I don't know what to do to solve this problem. Please let me know if more information is needed.
As commented by Shadow, you want to store the questions that were already asked to each user, regardless of the fact that they gave the correct answer or not. You don't need to store the questions that they did not yet answered.
Then, you can use the following query to pull out a random question that was not yet asked to a given user:
SELECT *
FROM questions q
WHERE NOT EXISTS (
SELECT 1
FROM user_questions uq
WHERE uq.id_user = :id_user AND uq.id_question = q.id_question
)
ORDER BY rand() LIMIT 1
Where parameter :id_user is the id of the current user.
For this to work, you need a large number of questions, because once a user has seen all possible questions, the above query will return an empty result set. Optionaly, you can store the date when they were asked each question in the user_questions table, and add a filter in the subquery to exclude only questions that have been seen recently.
SELECT *
FROM questions q
WHERE NOT EXISTS (
SELECT 1
FROM user_questions uq
WHERE
uq.id_user = :id_user
AND uq.id_question = q.id_question
AND uq.date_asked > NOW() - INTERVAL 30 DAY
)
ORDER BY rand() LIMIT 1
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have a MySQL that lists about 500 items. Users can register at the site and mark which item they have already. This is a simple php that reads the table and each row.
How should I structure the database so that each user can have a particular setting for each item?
So that when Bob opens his account, he sees he already has ITEM 1, but when Mary opens it, she'll have ITEM 1 as missing and she can see who has that ITEM 1 twice or more times so that she may contact whoever has more than 1 of an item?
You could do something like this:
Have a table that shows which users have what items...
Table Name: user_items
primary_key | user_id | item_number
-----------------------------------
1 | 1 | 1
2 | 1 | 1
3 | 1 | 2
4 | 2 | 2
or you could have something like this
primary_key | username | item_number
-----------------------------------
1 | bob | 1
2 | bob | 1
3 | bob | 2
4 | mary | 2
This table above shows that:
User 1 / Bob has:
item 1 x2
item 2
User 2 / Mary has:
item 2
You can create other table with three columns, foreign key for item, foreign key for user and amount of that item.
You will keep there information that Bob has ITEM 1.
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 a platform with multiple clients and each has a large set of demographics that I need to store. With this in mind, it seems I could do one of two things in MySQL. Either:
Option #1: Have a large table with everyone's demographics. For example:
Table: clientDemographics
id | clientID | firstName ....
1 | 34 | John ......
2 | 12 | Fred ......
Option #2: Split out each client to having their own table:
Table: client34_demographics
id | firstName ....
1 | John ......
Table: client12_demographics
id | firstName ....
1 | Fred ......
Are there any advantages to splitting the tables out by client (efficiency, security, scalability) or disadvantages? Which of these would be a better method? Thanks!
Your second example is not a good idea (creating a table for each demographic). Instead, I would go with something more "normalized" that contains unique identifiable information in the client table, and then additional meta data (demographics) as a lookup:
Table: Clients
ClientId | FirstName | LastName | Email
-------------------------------------------------
1 | John | Smith | jsmith#email.com
Table: Demographics
DemographicId | Name
-------------------------------------------------
1 | Gender
2 | Nationality
3 | Age
Table: Clients_Demographics
CDId | ClientId | DemographicId | Value
-------------------------------------------------
1 | 1 | 1 | Male
2 | 1 | 2 | American
3 | 1 | 3 | 27
In this way you can easily sort on demographic types, demographic values, clients, etc and all the while saving space in your database, increasing query performance, and keeping your data scalable. By scalable I mean, need to add another Demographic? Just add another row to the Demographics table and then associate a value in the Clients_Demographics table with a Client. If they value is not set (i.e. no row exists) then you know that value can be seen as empty in your forms until they actually set a value.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I want to make like following:
I have a page, where users can register..
And another page, with their profile..
mysql table look's like following:
USER
-----------------------------------------------
| id | name | age | about | registered | email|
-----------------------------------------------
INFO
--------------------------------------
| id | title | content | date | hits |
--------------------------------------
Informations about user are stored in USER database..
Now user want to add some "INFO" in their accound everyday..
when user add their info, there will be another page wich will show:
NAME REGISTERED with EMAIL (from USER database) added following
TITLE (from INFO database)
CONTENT (from INFO database)
DATE (from INFO database)
HITS (from INFO database)
I really dont know how to do that ..
Pls understand me.. Im newbie on PHP
Cheers!
Try this
SELECT u.email,i.title,i.content,i.date,i.hits FROM user u,info i where u.id = i.id;
Try the mySQL JOIN command that will bring data from two or more data tables together via a specific collaboration.
I can't see where you link the USER table to the INFO table.
My suggestion is that you must create another table to be able to connect the two tables. Something like:
---------------------
| USER_ID | INFO_ID |
---------------------
| 1 | 1 |
---------------------
| 1 | 2 |
---------------------
That way, you could make a JOIN between the two tables.
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 9 years ago.
Improve this question
how to use multi-keywords search with php and mysql ?
I have a product table like this
the keywords field is save the keyword id
| id | name | keyword_ids |
|112 | apple | 123,12,421,121|
|113 | phone | 23,14,12,1 |
and the keyword table like this
|id | name |
|1 | white |
|2 | eat |
I want use a product keywords field find the similar product, how can I do it?
If you're set on using this data structure, you can use FIND_IN_SET
SELECT * FROM `products` p
LEFT JOIN `keyword` k ON FIND_IN_SET(k.`id`, p.`keyword_ids`)
WHERE k.`name` IN (?,?,?)
What I'd recommend doing is actually from a many-to-many relation table linking a product to keywords eg:
product_has_keyword
product_id | keyword_id
------------------------
112 | 123
112 | 12
112 | 421
112 | 121
That way you can use index for a join (which will be much faster)