I have a database contains ID and name of my Staff (DATABASE 1):
--------------
| ID | name |
--------------
| 1 | Mr.AA |
| 2 | Mr.AB |
|... | ... |
| 78 | Mr.CZ |
--------------
Then my colleague has the staff absence database per day for 2 years (DATABASE 2):
Tablename: Table_for_Mr.AA
--------------------------
| ID | date | work |
--------------------------
| 1 | 2016-01-01 | Yes |
| 2 | 2016-01-02 | Yes |
| 3 | 2016-01-03 | No |
|... | ... | ... |
|730 | 2017-12-31 | Yes |
--------------------------
Due to our agreement, we hold each database ourselves (2 parties), so each database is stored in different server.
Lately I need to get the data from DATABASE 2 to be shown in my website and I can ask my colleague to make PHP file that return the array for each name (www.colleaguewebsite/staff/absence.php?name=Mr.AA).
I already made the new 'workstat' database (DATABASE 3) in my server with this detail:
---------------------------------
| ID | date | Name | work |
---------------------------------
| 1 | 2016-01-01 |
| 2 | 2016-01-02 |
| 3 | 2016-01-03 |
|... | ... |
---------------------------------
this is the best I can do:
$sourceURL = 'www.colleaguewebsite/staff/absence.php'
$sql1= $conn->query("select * FROM staff ");
while($row_1 = $sql1->fetch_array()){
$name= $row_1 ['name'];
//getting the absence detail from each staff
$json_1 = file_get_contents($sourceURL.'?name='.$name);
$data_1 = json_decode($json_1,true);
foreach($data_1 as $value_1){
$date = $value_1['date'];
$work = $value_1['work'];
//if the correspondence date is exist then update, otherwise add
$sql_2 = $conn->query("select * FROM workstat WHERE date='$date' AND name='$name");
if ($sql_2->num_rows > 0){
$update=$conn->query("UPDATE workstat set name='$name', work='$work' WHERE date='$date' ");
}else{
$addnew=$ob->query("INSERT INTO availability (date, name, work) VALUES ('$date', '$name', '$work'
}
}
}
However, I have some things that bothers:
The required time to execute this script is very long, mostly exceeding the 90 seconds time.
Dirty database. I will have 730 row of data (per day) for each name, so my database 3 will have 730 * 78 person = 56.940 rows with duplicate date (2017-01-01 ... 2017-12-31 for Mr.AA, 2017-01-01 ...2017-12-31 for Mr.AB, etc...).
How can I optimize my code in table design and loading time?
Another method than file_get_contents is okay, I hope it's still PHP.
Related
I have the following MySQL table which is structured like that:
| id | bonus0 |
Now I want to add the following data set:
| id | bonus0 | bonus1 | bonus2 | bonus3 |
| 10 | 4582 | 2552 | 8945 | 7564 |
As you can see the columns bonus1 - bonus3 arenĀ“t created yet.
How would a php script/ query look like which checks if enough columns are already available and if not which will create the missing ones with consecutive numbers at the end of the word "bonus"?
So in the example the columns bonus1 - bonus3 would be created automatically by the script.
In reality (I mean a normalized relational database) you should have 3 tables. Lets call them people, bonuses and bonus_to_person
people looks like:
+-----------------+------------+
| person_id | name |
+_________________+____________+
| 1 | john |
+-----------------+------------+
| 2 | frank |
+-----------------+------------+
bonuses Looks like
+----------------+--------------+
| bonus_id | amount |
+________________+______________+
| 1 | 1000 |
+----------------+--------------+
| 2 | 1150 |
+----------------+--------------+
| 3 | 1200 |
+----------------+--------------+
| 4 | 900 |
+----------------+--------------+
| 5 | 150 |
+----------------+--------------+
| 6 | 200 |
+----------------+--------------+
bonus_to_person Looks like
+----------------+-----------------+
| bonus_id | person_id |
+________________+_________________+
| 1 | 1 |
+----------------+-----------------+
| 2 | 2 |
+----------------+-----------------+
| 3 | 2 |
+----------------+-----------------+
| 4 | 1 |
+----------------+-----------------+
| 5 | 1 |
+----------------+-----------------+
| 6 | 1 |
+----------------+-----------------+
This way, any ONE person can have unlimited bonuses simply by INSERTING into bonuses with the amount, and INSERTING into bonus_to_person with the bonus_id and person_id
The retrieval of this data would look like
SELECT a.name, c.amount from people a
LEFT JOIN bonus_to_people b
ON a.person_id = b.person_id
LEFT JOIN bonuses c
ON c.bonus_id = b.bonus_id
WHERE a.person.id = 1;
Your result from something like this would look like
+------------+----+-------+
| name | amount |
+____________+____________+
| john | 1000 |
+------------+------------+
| john | 900 |
+------------+------------+
| john | 150 |
+------------+------------+
| john | 200 |
+------------+------------+
You should be using this normalized approach for any database that will continue growing -- Growing "deeper" than "wider" is better in your case ..
// Get existing columns of the table
// $queryResult = run SQL query using PDO/mysqli/your favorite thing: SHOW COLUMNS FROM `table`
// Specify wanted columns
$search = ['bonus0', 'bonus1', 'bonus2', 'bonus3'];
// Get just the field names from the resultset
$fields = array_column($queryResult, 'Field');
// Find what's missing
$missing = array_diff($search, $fields);
// Add missing columns to the table
foreach ($missing as $field) {
// Run SQL query: ALTER TABLE `table` ADD COLUMN $field INT
}
please assume this as the table
tablename- schemeOverview
| slno |schemename | amount| date2 | date3 | date4 ...daten |
|---------|-----------|-------|-------|-------|-------|----------|
| 1 | Cell | 1000 | DUE | DUE | | |
| 2 | Cell | 1000 | PAID | PAID | | |
| 3 | Cell | 1000 | DUE | DUE | DUE | |
| 4 | Cell | 1000 | PAID | PAID | | |
| 5 | Cell | 1000 | DUE | DUE | | |
I am trying to count the number of fields that have value as 'PAID' .
for example the count of PAID for slno-2 should be 2.
the number of date columns varies per table but I have the count of date columns for a table.So a better option would be to check all fields of the specified row(slno)
you can use this code
SELECT slno,count(*) AS paid
FROM schemeOverview
WHERE 'PAID' IN (date2, date3, date4,...,daten)
Well after a lot of brain drain I came up with this and It works!
$paidCount=0;
$sql2 = "SELECT * FROM " .$scheme_name. " WHERE slno = " .$recBookNo;
$result2 = mysqli_query($conn,$sql2);
if($result2)
{
while ($row19 = $result2->fetch_assoc())
{
foreach ($row19 as $key => $value)
{
if(($value)=='PAID')
{
$paidCount++;
}
}
}
}
You can use count()
SELECT count(date2) as cd2 FROM schemeOverview WHERE date2 = "PAID"
This will store the value into cd2, in this case 2
I am doing a script want to calculate how many row record before an user record when t1.status is 1.
My table is t1, and the data as below:
+------+---------+------------+----------+----------+
| ID | name | desc | status | time |
+------+---------+------------+----------+----------+
| 1 | ABB | | 1 | 0325 |
| 2 | CCD | | 1 | 0236 |
| 3 | EEF | | 1 | 0325 |
| 4 | GGG | | 1 | 0000 |
| 5 | HIJ | | 2 | 1234 |
| 6 | KKK | | 1 | 5151 |
+---------------------------------------------------+
I was thinking about the query is something like (query row where status = 1 AND stop when reach $userid)
I would like to output to show user (Let's say username is GGG) as:
$userid = 'GGG';
then my output will be
<table><tr><td>Queue: GGG You came in 4 place, in front of you still got 3 person in queue, please be patient</td></tr></table>
How to I do the right query to get the number 4 and 3 ?
Thank you.
You can try something like this hope it helps :-
SELECT count(*) as COUNT FROM t1 WHERE id < (SELECT id FROM t1 WHERE userid = $userid)
I need to synchronize two different booking calendars, beacause both calendars book the same room (or the same event).
So, if a client book a day (and hours) in calendar_01, this value (booked day and hours) will be automatically updated in calendars_02 (and vice versa).
It's important to update (and rewrite the new value) in order of the last time (most recent booking) without a continuous loop.
MySql DB
I'm using a plugin for this and in "calendars" database there is a table called "days", in this table I can see this:
+--------------+-------------+------------+------+----------------------------------------------------------+
| unique_key | calendar_id | day | year | data |
+--------------+-------------+------------+------+----------------------------------------------------------+
| 1_2014-08-20 | 1 | 2014-08-20 | 2014 | available h10-12; booked h12-14; in pending h14-16; |
| 2_2014-08-20 | 2 | 2014-08-20 | 2014 | available h 10 - 12; available h12-14; available h14-16; |
| 1_2014-08-21 | 1 | 2014-08-21 | 2014 | available h10-12; available-14; available h14-16; |
| 2_2014-08-21 | 2 | 2014-08-21 | 2014 | booked h10-12; booked h12-14; in pending h14-16; |
+--------------+-------------+------------+------+----------------------------------------------------------+
Simplification: column "data" contains the values (TEXT type) that record every rebooking, so:
+--------------+-------------+------------+------+--------------+
| unique_key | calendar_id | day | year | data |
+--------------+-------------+------------+------+--------------+
| 1_2014-08-20 | 1 | 2014-08-20 | 2014 | text value A |
| 2_2014-08-20 | 2 | 2014-08-20 | 2014 | text value B |
| 1_2014-08-21 | 1 | 2014-08-21 | 2014 | text value C |
| 2_2014-08-21 | 2 | 2014-08-21 | 2014 | text value D |
+--------------+-------------+------------+------+--------------+
I need to update the values of the same column "data", like this:
+--------------+-------------+------------+------+--------------+
| unique_key | calendar_id | day | year | data |
+--------------+-------------+------------+------+--------------+
| 1_2014-08-20 | 1 | 2014-08-20 | 2014 | text value A |
| 2_2014-08-20 | 2 | 2014-08-20 | 2014 | text value A |
| 1_2014-08-21 | 1 | 2014-08-21 | 2014 | text value D |
| 2_2014-08-21 | 2 | 2014-08-21 | 2014 | text value D |
| 1_2014-08-22 | 1 | 2014-08-22 | 2014 | text value X |
| 2_2014-08-22 | 2 | 2014-08-22 | 2014 | text value X |
| 1_2014-08-23 | 1 | 2014-08-23 | 2014 | text value Y |
| 2_2014-08-23 | 2 | 2014-08-23 | 2014 | text value Y |
+--------------+-------------+------------+------+--------------+
Many thanks in advance for any help!
I would strongly advice against syncing/Data Replication. You'd need to run a deamon 24/7, the risk of running into issues is much higher, it's also less eficient since It has to keep checking for new data in both tables which also means a delay for people to see their new bookings on the site. And it not so easy to debug when something does go wrong with the deamon.
The following solution is much easier to debug, more efficient. I would suggest you write abstract CRUD code for the data: Create, Read, Update and Delete. Create and Update are probably the ones you're most interested in, what you would do is something like this:
<?php
function create($id, $data)
{
$id = mysql_real_escape_string($id);
$data = mysql_real_escape_string($data['data']);
mysqL_query("INSERT INTO calendars (unique_key,data) VALUES('".$id."','".$data."')");
mysqL_query("INSERT INTO days (unique_key,data) VALUES('".$id."','".$data."')");
}
function update($id, $data)
{
$id = mysql_real_escape_string($id);
$data = mysql_real_escape_string($data['data']);
mysqL_query("UPDATE calendars SET data = '".$data."' WHERE unique_key = '".$id."'");
mysqL_query("UPDATE days SET data = '".$data."' WHERE unique_key = '".$id."'");
}
create('1_2014-08-20', array(
'data' => 'data here'
));
update('1_2014-08-20', array(
'data' => 'data here'
));
This is as simple as passing data into it. If you ever modify the SQL structure you can create a new abstraction set of functions/classes that follows the new database structure and it's as easy as swapping out an include.
I have three tables: years, employees, positions. Suppose that I already have these data in those tables.
years:
----------------
| id | name |
----------------
| 1 | 2011 |
----------------
positions:
------------------------------
| id | name | year_id |
------------------------------
| 1 | Director | 1 |
| 2 | Manager | 1 |
------------------------------
employees:
---------------------------------------------------------
| id | name | position_id | year_id |
---------------------------------------------------------
| 1 | Employee A (Director) | 1 | 1 |
| 2 | Employee B (Manager) | 2 | 1 |
---------------------------------------------------------
========================================
The years table is a central point.
If I insert a new year record, I must also copy all positions and employees which are related to the previous year.
So if I insert year 2012 into the years table, the data is suppose to be like this:
years:
----------------
| id | name |
----------------
| 1 | 2011 |
| 2 | 2012 |
----------------
positions:
------------------------------
| id | name | year_id |
------------------------------
| 1 | Director | 1 |
| 2 | Manager | 1 |
| 3 | Director | 2 |
| 4 | Manager | 2 |
------------------------------
employees:
---------------------------------------------------------
| id | name | position_id | year_id |
---------------------------------------------------------
| 1 | Employee A (Director) | 1 | 1 |
| 2 | Employee B (Manager) | 2 | 1 |
| 3 | Employee A (Director) | 3 (?) | 2 |
| 4 | Employee B (Manager) | 4 (?) | 2 |
---------------------------------------------------------
NOTICE the question marks in the third and fourth row of employees table.
I use these queries to insert a new year and copy all related positions and employees:
// Insert new year record
INSERT INTO years (name) VALUES (2012);
// Get last inserted year ID
$inserted_year_id = .......... // skipped
// Copy positions
INSERT INTO positions (name, year_id) SELECT name, $inserted_year_id AS last_year_id FROM positions WHERE year_id = 1;
// Copy employees
INSERT INTO employees (name, position_id, year_id) SELECT name, position_id, $inserted_year_id AS last_year_id FROM employees WHERE year_id = 1;
The problem is at copying employees query. I can't find a method to get or track the new ID of positions.
Is there a simple method to do this?
Thank you very much.
Your data model is seriously flawed and probably needs a complete overhaul, but if you insist on copying the data like you describe, this should do the trick:
// Copy employees
INSERT INTO employees (name, position_id, year_id)
SELECT name, new_positions.id, $inserted_year_id AS last_year_id
FROM employees
JOIN positions AS old_positions ON old_positions.id = employees.position_id
AND old_positions.year_id = employees.year_id
JOIN positions AS new_positions ON new_positions.name = old_positions.name
AND new_positions.year_id = $inserted_year_id
WHERE employees.year_id = 1
I think you should read about database normalization. Copying the data leads to maintenance issues and erroneous reporting.
If you went with a different design like the following, then there would be nothing to insert, until an employee changes position, is terminated, or a position is discontinued. There are plenty of other ways to approach this, too, but you should minimize redundancy (i.e. have only one copy of each Employee), and then keep track of data that changes over time, separately. Also read about foreign keys before you try to implement something like this.
positions:
-- If you are keeping track of the years that each position is active,
-- using dates provides simplicity. Note: this design assumes that positions
-- are never reactivated after being deactivated.
------------------------------------------------
| id | name | DateActive | DateInactive |
------------------------------------------------
| 1 | Director | 01/01/2011 | |
| 2 | Manager | 01/01/2011 | |
------------------------------------------------
employees:
---------------------------------------------------------------
| id | name | DateHired | DateTerminated |
---------------------------------------------------------------
| 1 | Employee A | 01/01/2011 | |
| 2 | Employee B | 01/01/2011 | |
| 3 | Employee C | 01/01/2011 | 10/01/2012 |
---------------------------------------------------------------
EmployeePositionRelationships
--If you are keeping track of time that each employee held a position
-- Employee A has been a Director since 1/1/2011
-- Employee B was a Manager from 1/1/2011 to 10/6/2012. Then they became a Director
-- Employee B was a Manager from 1/1/2011 to 10/1/2012. Then they were terminated
--------------------------------------------------------
EmployeeId | PositionId | DateStarted | DateEnded |
--------------------------------------------------------
1 | 1 | 01/01/2011 | |
2 | 2 | 01/01/2011 | 10/6/2012 |
3 | 2 | 01/01/2011 | 10/1/2012 |
2 | 1 | 10/6/2012 | |
--------------------------------------------------------