multi to multi relational database not working - php

I am studying relational databases on my own and after reading some articles I am having trouble implementing them.
Due to my lack of experience with relational databases I believe that my error can be in any of the following 3 (if not in all 3) steps:
Table Creaton
Value Insertion
Query call
I should point out that there aren't any syntax errors or something like that.
We are talking about an error in my understanding so I will approach the problem in detail.
The tables:
For the abstraction I have chosen to have people and the drinks which they like (1 person can like many drinks, one drink can be liked by many people.
Here is the code for the table People (It has 2 names, and a primary key):
CREATE TABLE `People`
(
`PeopleID` INT NOT NULL AUTO_INCREMENT,
`FirstName` VARCHAR(25),
`LastName` VARCHAR(25) NOT NULL,
PRIMARY KEY (`PeopleID`)
)
Here is the table Drinks (Name of drink, some number in menu or something like that for example and the primary key)
CREATE TABLE `Drinks`
(
`DrinksID` INT NOT NULL AUTO_INCREMENT,
`Code` VARCHAR(10) CHARACTER SET ascii COLLATE ascii_general_ci NOT NULL,
`Name` VARCHAR(100) NOT NULL,
PRIMARY KEY (`DrinksID`)
)
And lastly the table PeopleDrinks:
CREATE TABLE `PeopleDrinks`
(
`DrinksID` INT NOT NULL default 1,
`PeopleID` INT NOT NULL default 1,
PRIMARY KEY (`DrinksID`,`PeopleID`)
)
Note: from here I saw the example given for the third table and pretty much copied it.
Value insertion:
People input:
INSERT INTO People (FirstName, LastName) VALUES ('John', 'Smith')
INSERT INTO People (FirstName, LastName) VALUES ('Sam', 'Johnson')
INSERT INTO People (FirstName, LastName) VALUES ('Michael', 'Morgan')
Drinks input:
INSERT INTO Drinks (Code, Name)VALUES ('#543', 'Beer')
INSERT INTO Drinks (Code, Name) VALUES ('#132', 'Vodka')
INSERT INTO Drinks (Code, Name) VALUES ('#123', 'Wine')
PeopleDrinks input:
Note: The id of the drinks are 4, 5 and 6. Not 1, 2, 3.
INSERT INTO PeopleDrinks (DrinksID, PeopleID) VALUES (1, 4)
INSERT INTO PeopleDrinks (DrinksID, PeopleID) VALUES (2, 5)
INSERT INTO PeopleDrinks (DrinksID, PeopleID) VALUES (3, 6)
Above we give as favorite one drink to every person. Below we will make one person like all the drinks:
INSERT INTO PeopleDrinks (DrinksID, PeopleID) VALUES (1, 5)
INSERT INTO PeopleDrinks (DrinksID, PeopleID) VALUES (1, 6)
And finally the query call:
//Consider $link an established, working connection to the database
$result = mysqli_query($link, "SELECT
`People`.*
FROM
`People`
JOIN
`PeopleDrinks` ON `People`.`PeopleID` = `PeopleDrinks`.`PeopleID`
WHERE
`PeopleDrinks`.`DrinksID` = 5");
while($row = mysqli_fetch_array($result))
{
echo $row['FirstName'] . " " . $row['LastName'];
echo "<br/>";
}
The above code, should print the name of anyone who likes the drink with id 5. I don't get an error, but I don't get any output.

You said the id of the drinks are 4,5,6
In the INsert you have used
INSERT INTO PeopleDrinks (DrinksID, PeopleID)VALUES (1, 4)
INSERT INTO PeopleDrinks (DrinksID, PeopleID) VALUES (2, 5)
INSERT INTO PeopleDrinks (DrinksID, PeopleID) VALUES (3, 6)
The drink ids(first number) are 1,2,3 instead of 4,5,6 or something like that. I think your order is wrong.

Your many to many table needs foreigns key :
CREATE TABLE `PeopleDrinks` (
`DrinksID` INT NOT NULL,
`PeopleID` INT NOT NULL,
PRIMARY KEY (`DrinksID`,`PeopleID`),
FOREIGN KEY (`DrinksID`) REFERENCES `Drinks`(`DrinksID`),
FOREIGN KEY (`PeopleID`) REFERENCES `People`(`PeopleID`)
)
Remove the default values of DrinksID and PeopleID, this make no sense.
Then when you do your insert, you will be sure that the data you are trying to insert are right.
I think that your insert are false. When you say that your IDs are what they are, you are wrong. This is particulary true if your are doing a lot of inserts and deletes. Autoincrement will keep going forever, and you can't be sure. With Foreign Keys, the RDBMS will tell you if there is something wrong with your relations.
Anyway, your query looks great, so no problem for that.

Related

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.

Adding lists of data to MySQL

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)');

calculate different values from mysql to php tables

How can I compile the results of various calculations from data gathered from different tables ? Present summaries in php table. I have people in a table that is linked to two other tables. It will be sorted by department how many people are women and men in number and percentage . How much they earn in total per department in average. How big women's pay is a percentage of men and how great the wage distribution is the max and min values ​​for men and women.
These code is used to get all the data.
I have added some value manually just for clarification in this picture.
I have struggled with this for some time now and see no solution. Grateful for the help !
Please see below, it's not exactly as you need, since I don't know a structure of tables, but you will get an idea:
CREATE TABLE `usergroup` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB;
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`sex` enum('yes','no') NOT NULL DEFAULT 'yes',
`salary` decimal(10,2) DEFAULT NULL,
`userGroupId` int(11) DEFAULT NULL,
`name` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB;
insert into `usergroup`(`id`,`name`) values (1,'Sales');
insert into `usergroup`(`id`,`name`) values (2,'Support');
insert into `usergroup`(`id`,`name`) values (3,'Managment');
insert into `usergroup`(`id`,`name`) values (4,'Others');
insert into `user`(`id`,`sex`,`salary`,`userGroupId`,`name`) values (1,'yes',20000.00,1,'Scott');
insert into `user`(`id`,`sex`,`salary`,`userGroupId`,`name`) values (2,'yes',30000.00,1,'Peter');
insert into `user`(`id`,`sex`,`salary`,`userGroupId`,`name`) values (3,'no',20000.00,1,'Mike');
insert into `user`(`id`,`sex`,`salary`,`userGroupId`,`name`) values (4,'yes',100000.00,2,'Senior');
insert into `user`(`id`,`sex`,`salary`,`userGroupId`,`name`) values (5,'no',50000.00,2,'Junior');
insert into `user`(`id`,`sex`,`salary`,`userGroupId`,`name`) values (6,'yes',75000.00,2,'Middle');
insert into `user`(`id`,`sex`,`salary`,`userGroupId`,`name`) values (7,'yes',250000.00,3,'King');
insert into `user`(`id`,`sex`,`salary`,`userGroupId`,`name`) values (8,'yes',300000.00,3,'Ace');
insert into `user`(`id`,`sex`,`salary`,`userGroupId`,`name`) values (9,'no',200000.00,3,'Queen');
insert into `user`(`id`,`sex`,`salary`,`userGroupId`,`name`) values (10,'yes',100000.00,3,'Jack');
insert into `user`(`id`,`sex`,`salary`,`userGroupId`,`name`) values (11,'no',400000.00,3,'LadyJoker');
Query to get report. The subquery is selecting a data with stats values, which are used in the outer query for calculation:
select analytic.userGroupId,
analytic.name,
analytic.countMen,
analytic.countWomen,
round((analytic.countMen / analytic.countMisc)*100, 2) as percentMen,
round((analytic.countWomen / analytic.countMisc)*100, 2) as percentWomen,
round((analytic.maxSalaryMen + analytic.minSalaryMen)/2, 2) as basicSalaryMen,
round((analytic.maxSalaryWomen + analytic.minSalaryWomen)/2, 2) as basicSalaryWomen,
round(
(analytic.maxSalaryMen + analytic.minSalaryMen +
analytic.maxSalaryWomen + analytic.minSalaryWomen
)/4,
2)
as basicSalaryMisc,
if(analytic.maxSalaryMen + analytic.minSalaryMen = 0, 100,
round((analytic.maxSalaryWomen + analytic.minSalaryWomen)/(analytic.maxSalaryMen + analytic.minSalaryMen), 2)
) as salaryBasicWomenPercentOfMen,
analytic.maxSalaryMen,
analytic.minSalaryMen,
analytic.maxSalaryWomen,
analytic.minSalaryWomen
from (
select ug.id as userGroupId,
ug.name,
sum(if(u.sex='yes', 1, 0)) countMen,
sum(if(u.sex='no', 1, 0)) countWomen,
count(*) countMisc,
max(if(u.sex='yes', u.salary, null)) maxSalaryMen,
min(if(u.sex='yes', u.salary, null)) minSalaryMen,
max(if(u.sex='no', u.salary, null)) maxSalaryWomen,
min(if(u.sex='no', u.salary, null)) minSalaryWomen
from userGroup ug left join user u
on u.userGroupId = ug.id
group by ug.id, ug.name
order by ug.name
) analytic

How to total up entries in a database

I'm using a MySQL database which has a 'Staff' column. Example:
ID BUSINESS STAFF
1 Business 1 Bob
2 Business 2 Bill
3 Business 3 Paul, Bill
4 Business 4 Bob
I'm aiming to create a pie chart showing how many businesses each member of staff has, using the Google Charts API, and I'm having trouble counting total amounts.
The chart uses the formatting:
var data = google.visualization.arrayToDataTable([
['Staff', 'Business'],
['Bob', 2],
['Bill', 2],
['Paul', 1]
]);
How would I go about echoing a count of these lines? I've spent a fun 40 minutes messing up COUNT queries, with absolutely no joy.
There is a common theme in the comments here and that is normalization. As a rule it is A Bad Thing to represent multiple values as a comma-separated list in a single column.
Here is a working example of an alternate DB design that should get you going in the right direction:
create table staff
(
id int unsigned not null primary key auto_increment,
staffName varchar(250),
unique key `staffUIdx1` (staffName)
) ENGINE=InnoDB;
create table business
(
id int unsigned not null primary key auto_increment,
businessName varchar(250),
unique key `staffUIdx1` (businessName)
) ENGINE=InnoDB;
CREATE TABLE staffBusiness
(
id int unsigned not null primary key auto_increment,
staffId int unsigned not null,
businessId int unsigned not null,
unique key `staffBusinessUIdx1` (staffId,businessId),
CONSTRAINT `fk_staffBusiness_staff1` FOREIGN KEY (`staffId`) REFERENCES `staff` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_staffBusiness_business1` FOREIGN KEY (`businessId`) REFERENCES `business` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB;
insert into staff (staffName) values ('Bob');
insert into staff (staffName) values ('Bill');
insert into staff (staffName) values ('Paul');
insert into staff (staffName) values ('George');
insert into business (businessName) values ('Business 1');
insert into business (businessName) values ('Business 2');
insert into business (businessName) values ('Business 3');
insert into business (businessName) values ('Business 4');
insert into staffBusiness (staffId,businessId)
select s.id,b.id from staff s join business b
where s.staffName = 'Bob' and b.businessName in ('Business 1','Business 4');
insert into staffBusiness (staffId,businessId)
select s.id,b.id from staff s join business b
where s.staffName = 'Bill' and b.businessName in ('Business 2','Business 3');
insert into staffBusiness (staffId,businessId)
select s.id,b.id from staff s join business b
where s.staffName = 'Paul' and b.businessName in ('Business 3');
...and then the query would look like this:
select staffName as Staff,count(sb.id) as Business
from staff s
left outer join staffBusiness sb on s.id = sb.staffId
group by staffName;
I've included a 4th member of staff called 'George' to show that a normalized approach allows you to have members of staff with no business too.
I develop this code according your question ,you should change according your table and fields also change server configuration server name,user,password,database
$server="server";
$user="user";
$password="password";
$database="database";
$cid=mysql_connect($server,$user,$password);
mysql_select_db($database,$cid);
$query="select * from table";
$rs=mysql_query($query,$conn);
$val=0;
while($row=mysql_fetch_array($rs))
{
$data=$row['business'];
$val=$val+trim(str_replace("Business","", "$data"));
}
$total=$val;
I used this code and i found total ;

mysql insert problem

ive got a really weird problem. i have no clue why its not working. i create an user and get the id and insert a row based on that id in another table. the row gets inserted with that id but the other values however for that row are not inserted!
$user_id = mysqli_insert_id($this->connection);
$query = "INSERT INTO selections
(user_id, language_id, country_id, region_id, city_id, gender_id, age_id, category_id)
VALUES ($user_id, 1, 1, 0, 0, 0, 20, 0)";
so the user_id gets inserted, but not the other values (they are all 0 in the table). i have really checked the columns and deleted all foreign keys to debug this problem. but i have no clue at all.
the columns are all INT. the weird part is sometime when i replace $user_id with a literal number it works, sometimes it doesnt. but the row is always created. and i have checked that $user_id is an integer.
i know this is a hard problem and that it can be caused of a lot of things, but i have tried to solve this tiny issue for 3 hours now. so would be great if someone just gave me something i could do to debug this problem.
UPDATE: even when i have set default values and just insert the first column (user_id) it doesnt work. every other field is 0. So weird!
| selections | CREATE TABLE `selections` (
`user_id` int(11) NOT NULL,
`language_id` int(11) NOT NULL DEFAULT '1',
`country_id` int(11) NOT NULL DEFAULT '1',
`region_id` int(11) NOT NULL DEFAULT '0',
`city_id` int(11) NOT NULL DEFAULT '0',
`gender_id` int(11) NOT NULL DEFAULT '0',
`age_id` int(11) NOT NULL DEFAULT '0',
`category_id` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
$query = "INSERT INTO selections
(user_id)
VALUES ('$user_id')";
the user_id shows 178 and other fields are 0:(
UPDATE:
It worked in the sql command line. but not in php. but mysqli generated no error and the row was indeed inserted but why are the other fields 0?
ANSWER: My fault. i had a jquery script that changed it back to 0 0 0 0 0 0 0. There's a lot of AJAX on my page so it was tricky to find it...sorry my bad!
When you run into situations like this, print the query to screen before it is executed:
$query = "INSERT INTO ...";
echo $query
Try:
$query = "INSERT INTO selections
(user_id, language_id, country_id, region_id, city_id, gender_id, age_id, category_id)
VALUES
({$user_id}, 1, 1, 0, 0, 0, 20, 0)";
You need to wrap PHP variables in {} when referencing them in SQL string statements.
Use Your DEFAULT Constraints
If you have defaults then you don't need to set the values in your INSERT statement:
INSERT INTO selections
(user_id)
VALUES
({$user_id})
Referencial Integrity
You're getting the last inserted id and using it in a subsequent insert into another table, but you don't have a foreign key defined on the user_id column to ensure that the value going into that column actually exists in the other table. If you provide the name of the table & column you are getting for your last insert id, I'll provide the ALTER TABLE statement.
$query = "INSERT INTO selections
(user_id, language_id, country_id, region_id, city_id, gender_id, age_id, category_id)
VALUES ('$user_id', 1, 1, 0, 0, 0, 20, 0)";
Single quotes around $user_id might do it.

Categories