Adding lists of data to MySQL - php

I have some data to add to my database, I'm not sure what my table schema should be. I have an id number for each specific user, 4 categories of games, and a possible number of items (0+) each user has for each game. I want to set a table for each game with the categories ->id and ->items, so I can save the list of user id's in the table, with the items they have for that game.
I can't seem to get it to work, I think because of the dynamic number of items for each user. Is it possible for me to achieve my above mentioned table schema? Why not/how?
I have been trying:
foreach ($json->rgDescriptions as $mydata)
{
$sql = $dbh->prepare('INSERT INTO user_items_tf2 (items) VALUES (:item) WHERE steam_id = :steamid');
$sql->bindParam(':item', $mydata->name);
$sql->bindParam(':steamid', $steamprofile['steamid']);
$sql->execute();
}

There are numbers of ways to do this but one which is very flexible and seems to answer your questions would be this.
-- Players
CREATE TABLE player
(`id` int primary key auto_increment, `name` varchar(255))
;
-- Games
CREATE TABLE game
(`id` int primary key auto_increment, `name` varchar(255))
;
-- Items and what game they belong to
CREATE TABLE item
(`id` int primary key auto_increment, `game_id` int, `name` varchar(255))
;
-- What games players are playing
CREATE TABLE player_game
(`player_id` int, `game_id` int)
;
-- What items players have
CREATE TABLE player_item
(`player_id` int, `item_id` int, index(`player_id`))
;
If you never needed to ask the question which users had a given item you could skip the player_item table and stuff the data (as JSON for instance) of their items into a column of the player table with a blob type.

$sql = $dbh->prepare('INSERT INTO user_items_tf2 (items, steam_id) VALUES (:item, :steamid)');

Related

Maintaining/updating sets of associations

Suppose we have these tables:
CREATE TABLE meetings (
id INT AUTO_INCREMENT,
# other fields
PRIMARY KEY(id)
);
CREATE TABLE participants (
id INT AUTO_INCREMENT,
# other fields
PRIMARY KEY(id)
);
CREATE TABLE meetings_participations (
meeting INT, # for joining on meetings.id
participant INT, # for joining on participants.id
PRIMARY KEY(meeting,participant)
);
Given a particular $meeting_id and an array $participant_ids of participants in that meeting, what is a good way to update the table meetings_participations?
I've considered
DELETE FROM meetings_participations
WHERE meeting = $meeting_id;
INSERT INTO meetings_participations
(meeting, participant)
VALUES
$participations;
where, in PHP
$participations = join(',', array_map(function($p){
return "($meeting_id,$p)";
}, $participant_ids));
but I wonder whether it wouldn't be better to try to conserve existing records (rather than deleting unconditionally and re-inserting). Is there a better approach to the general problem?

Inserting data into temporary mysql table with case

Consider the following example table:
CREATE TABLE items
(`id` int, `productName` varchar(7), `price_new` decimal(10,2), `price_used` varchar(55))
;
INSERT INTO items
(`id`, `productName`, `price_new`, `price_used`)
VALUES
(1, 'cup', 10.50, 5.50),
(2, 'plate', 9.50, 4.50),
(3, 'mug', 8.50, 3.50)
;
For an application I wish to create a temporary table based on what the user has.
The user would have say a 'cup' with condition of 'new'. (stored in a php array)
My desired temporary table would be something like:
CREATE TEMPORARY TABLE IF NOT EXISTS tempTable (
`id` INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
`name` varchar(7) NULL,
`itemCondition` varchar(7) NULL,
`price` decimal(10,2) NULL
);
My question is this:
If I create this temporary table how could I insert 'cup' and 'new' with MySQL selecting the price from the items table based on the itemCondition?
psuedocode:
Insert:
(1, 'cup', (
if itemCondition == 'new'
(select Items.price_new where items.productName = 'cup')
ELSE
(select Items.price_used where items.productName = 'cup')
Thank you for your assistance.
The problem is that until you insert the data into the temptable, sql will not know the value of the imtemCondition field. The value 'new' is supplied in the insert statement itself as a fixed value.
You will need either stored procedure that selects the price based on a parameter value and then insert that price into the temporary table, or you can implement the same logic using php.
Steps:
Provide user id, name, and item condition.
Select the price based on the item condition.
Insert all data to the temporary table.

Assign a unique ID to each name in a table where names are repeated

I have a table that contains millions of sales records and looks like this:
CREATE TABLE `sales` (
`dollar_amount` INT NULL,
`transaction_date` DATE NULL,
`company_name` VARCHAR(45) NULL,
`company_id` INT NULL);
The first three columns are populated with data. I would like to insert data into the company_id column that will identify each company with an auto_incremented integer. I plan to use the company_id field as a foreign key referencing another table that will contain each company's details. Many companies have multiple transactions, so the code needs to assign the same company_id to each row in the sales table with a matching company_name.
Is there a way to do this using only MySQL?
First, I'd recommend creating the company table:
CREATE TABLE company (
company_id INT NOT NULL AUTO_INCREMENT,
company_name VARCHAR(45),
PRIMARY KEY(company_id));
Then insert the companies from your sales data:
INSERT INTO company (company_name)
SELECT distinct company_name
FROM sales;
Finally, update your sales table with a join to get the company_id:
UPDATE sales s
JOIN company c ON s.company_name = c.company_name
SET s.company_id = c.company_id;
SQL Fiddle Demo
You should also remove the company_name field from the sales table since this is now stored in the company table.
To define an auto incremented integer, you just use the AUTO_INCREMENT keyword. However, if you define any columns as auto_increment, you must also make that column your primary key. Which, in this case, would make sense in order for it to be a foreign key elsewhere.
Try this:
CREATE TABLE `sales` (
`dollar_amount` INT NULL,
`transaction_date` DATE NULL,
`company_name` VARCHAR(45) NULL,
`company_id` INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(company_id);
SQL Fiddle

wordpress plugin sql setup

I have set up four additional tables for my plugin to use what I am trying to do is take a name and assign it a ID then use this data to populate drop down menus with a name and the same for class and position I am unsure as to how to do this correctly this is what i have so far.
$sql = "CREATE TABLE $tableName (
recordID int NOT NULL AUTO_INCREMENT,
PRIMARY KEY(recordID),
driverID int,
driverName varchar(30),
classID int,
driverClass varchar(20),
posID,
driverPosition varchar(6),
trackName varchar(30),
raceDate date
);";
$sql = "CREATE TABLE $driverTableName (
driverID int NOT NULL AUTO_INCREMENT,
PRIMARY KEY(driverID),
driverName varchar(30)
);";
$sql = "CREATE TABLE $classTableName (
classID int NOT NULL AUTO_INCREMENT,
PRIMARY KEY(classID),
className varchar (20)
);";
$sql = "CREATE TABLE $posTableName (
posID int NOT NULL AUTO_INCREMENT,
PRIMARY KEY(posID),
posName varchar(6)
);";
The bottom three tables will store the data I want to populate the drop down boxes to create a record with I am unsure as to how to link them to the top table where this record will be stored.
This is pretty much a indexing issue. If you are going to access the database separately to the standard calls that Wordpress provides, you should at the very least use http://codex.wordpress.org/Class_Reference/wpdb as it will save you some coding time.
The rest of it is a MySQL question. (Assuming you are using MySQL) In how to properly index the data together and then parsing the data as it comes in.

Relational tables: a 3rd table with relations

I'm building a php page that will show an specific banner when the user enters his/her phone number in a form field.
So here's my logic for the database:
Table phones with fields: id, name of user and phone number.
Table banners with fields: id, banner's name/title and banner (path to the image).
Table relation with fields: here's where the phone number should be related to a banner and where I need your help :)
And here's my logic for the php page:
-form gets the phone number
-I query the data base
-I show the banner related to the phone number entered in the form.
Below is the code for the table creation so far .. as you'll see don't know how to advance.
Thanks a million
CREATE TABLE phones(
id_phone INT NOT NULL AUTO_INCREMENT,
nombre VARCHAR(30),
number INT (9),
PRIMARY KEY (id_phone)
) TYPE = INNODB;
CREATE TABLE banners (
id_banners INT NOT NULL AUTO_INCREMENT,
id_phone INT NOT NULL,
name VARCHAR(250),
banner VARCHAR(250),
PRIMARY KEY(id_phone),
INDEX (id_phone),
FOREIGN KEY (id_phone) REFERENCES clientes(id_phone)
) TYPE = INNODB;
From your question, it seems that each phone number has only one banner associated with it. Therefore, delete the banners.id_banners field and add a phones.id_banners
Then to select the data, you can do a JOIN:
SELECT phones.id_phone, phones.nombre, phones.number,
banners.id_banners, banners.name, banners.banner
JOIN banners ON phones.id_banners = banners.id_banners
WHERE phone = '123-456-789'
First of all what's the question?
What can I mention at this step is, does the relation between this entities is one to one? If so, you'd better put all this data into one single table.
++
CREATE TABLE phones(
phone_id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(30),
number INT (9),
PRIMARY KEY (id_phone)
) TYPE = INNODB;
CREATE TABLE banners (
banner_id INT NOT NULL AUTO_INCREMENT,
phone_id INT NOT NULL,
name VARCHAR(250),
banner VARCHAR(250),
PRIMARY KEY(id_phone),
INDEX (id_phone),
FOREIGN KEY (phone_id) REFERENCES clientes(phone_id)
) TYPE = INNODB;
and the query would be:
$query = "SELECT * FROM clients LEFT JOIN banners USING(phone_id) WHERE clients.number='.$number.'";

Categories