项目作者: bardakcib

项目描述 :
A Simple DBMS - Database Management System using MySQL & PHP
高级语言: SQL
项目地址: git://github.com/bardakcib/Database-Management-Systems.git
创建时间: 2021-06-02T12:07:15Z
项目社区:https://github.com/bardakcib/Database-Management-Systems

开源协议:

下载


DBMS

A Simple Database Management System using MySQL & PHP





Our goal in this project is to create some reporting pages for a bookstore.

To run this project;

  1. Download ammps ( software stack of Apache, Mysql & PHP )
  2. After installation, copy all files to “C:\Program Files\Ampps\www\” ( this is default for me )
  3. Then go to “http://localhost/CSE348/install.php“ using your web browser

    Before running the project, Please go to MYSQL —> Edit —> Preferences —> SQL Editor and set to connection read timeout to at least 60. Because we will be running some scripts to simulate sales transactions on database side

To complete this project I have used :

  • VSCode for PHP and SQL codes
  • MySQL WorkBench ( with ammps, mysql will be installed but I don’t like the browser gui. So I have installed the workbench, workbench is much more capable )
  • For database connections I have used PDO - PHP Data Objects
  • To read and upload .csv files to database I have used “LOAD DATA INFILE” command.

To use this command you may need to run this sql command to check default folder path

  1. - SHOW VARIABLES LIKE "secure_file_priv";

To change it; go to “C:\Program Files\Ampps\mysql\my.ini” file and find “# SERVER SECTION” then add the below line under this section :

  1. - secure_file_priv="C:/Program Files/Ampps/www/CSE348"

Modified my.ini file :

After configurations, a main page like below should welcome you :

Invoice Report :

Sales Income Report :

Sales Report :

Best Worst Salesman :

Query For Reading From .csv Files

  1. LOAD DATA INFILE 'C:\\Program Files\\Ampps\\www\\CSE348\\turkey.csv'
  2. IGNORE INTO TABLE temp
  3. FIELDS TERMINATED BY ';'
  4. ENCLOSED BY ''
  5. LINES TERMINATED BY '\n'
  6. IGNORE 1 LINES
  7. (@col1, @col2, @col3)
  8. set district_name = TRIM(@col1),
  9. city_name = TRIM(@col2),
  10. branch_name = TRIM(@col3);

Cursor For Simulating Random Sales Transactions

  1. DROP PROCEDURE IF EXISTS procedure_SimulateSales;
  2. SET @row_number = 0;
  3. -- DELIMITER $$
  4. -- CREATE PROCEDURE procedure_SimulateSales()
  5. CREATE DEFINER=`root`@`localhost` PROCEDURE `procedure_SimulateSales`()
  6. BEGIN
  7. DECLARE done INT DEFAULT FALSE;
  8. DECLARE branchCount INT DEFAULT (Select Count(1) from branch);
  9. DECLARE customerID INT DEFAULT 0;
  10. DECLARE branchID INT DEFAULT 0;
  11. DECLARE stockID INT DEFAULT 0;
  12. DECLARE bookAmount INT DEFAULT 0;
  13. DECLARE totalStock INT DEFAULT 0;
  14. DECLARE minDate DATETIME DEFAULT '2020-04-30 14:53:27';
  15. DECLARE maxDate DATETIME DEFAULT '2021-04-30 14:53:27';
  16. DECLARE c1 CURSOR FOR
  17. -- Generate random number between 40 and 500 for every branch
  18. SELECT id as customerID,
  19. CASE
  20. WHEN @row_number < branchCount THEN @row_number:=@row_number + 1
  21. ELSE @row_number:=@row_number + 1 - branchCount
  22. END as branchID,
  23. (FLOOR( 10 + RAND( ) *10 )) AS bookAmount
  24. FROM bedirhan_bardakci.tampRandomCustomers;
  25. DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE; -- not found olursa true yap
  26. CREATE TABLE IF NOT EXISTS tampRandomCustomers (
  27. id INT(6) NULL
  28. );
  29. Delete from bedirhan_bardakci.tampRandomCustomers;
  30. INSERT INTO bedirhan_bardakci.tampRandomCustomers (id )
  31. SELECT id
  32. FROM bedirhan_bardakci.customer
  33. order by RAND();
  34. OPEN c1;
  35. WHILE NOT done DO
  36. FETCH NEXT FROM c1 INTO customerID, branchID, bookAmount;
  37. SELECT Count(1) into totalStock FROM bedirhan_bardakci.stock where branch_id = branchID and isSold = 0;
  38. IF (done = FALSE ) THEN -- Prevent Last row of inner cursor fetched twice
  39. WHILE bookAmount > 0 and totalStock > 0 DO
  40. SELECT id
  41. into stockID
  42. FROM bedirhan_bardakci.stock
  43. where branch_id = branchID
  44. and isSold = 0
  45. LIMIT 1;
  46. INSERT INTO bedirhan_bardakci.sale (customer_id, salesman_id, stock_id, amount, saledate)
  47. VALUES (
  48. customerID,
  49. (select id from bedirhan_bardakci.salesman where branch_id = branchID order by RAND() LIMIT 1),
  50. stockID,
  51. 1,
  52. (SELECT TIMESTAMPADD(SECOND, FLOOR(RAND() * TIMESTAMPDIFF(SECOND, minDate, maxDate)), minDate))
  53. );
  54. Update bedirhan_bardakci.stock set isSold = 1 where id = stockID;
  55. SET bookAmount = bookAmount - 1;
  56. SET totalStock = totalStock - 1;
  57. END WHILE;
  58. END IF;
  59. END WHILE;
  60. CLOSE c1;
  61. DROP TABLE IF EXISTS tampRandomCustomers;
  62. END;
  63. -- END$$
  64. -- DELIMITER ;
  65. CALL procedure_SimulateSales();

PHP PDO Database Connection Sample

  1. $mysql_host = "localhost";
  2. $mysql_user = "root";
  3. $mysql_password = "mysql";
  4. $db = new PDO("mysql:host=$mysql_host", $mysql_user, $mysql_password);

PHP PDO Data Fetch Sample

  1. $query2 = file_get_contents(__DIR__ . "\question3_C_1.sql");
  2. $query2 = $db->prepare($query2);
  3. $query2->bindParam(':param_id', $selected);
  4. $query2->execute();
  5. $tempRowIndex = 1;
  6. $tempIncome = 0;
  7. $tempAmount = 0;
  8. foreach ($query2 as $row2) {
  9. echo "<tr>";
  10. echo "<td>" . $tempRowIndex . "</td>";
  11. echo "<td>" . $row2['customerName'] . " " . $row2['customerSurnmae'] . "</td>";
  12. echo "<td>" . $row2['totalAmount'] . " pcs</td>";
  13. echo "<td>" . $row2['totalIncome'] . " TL</td>";
  14. echo "</tr>";
  15. $tempIncome = $tempIncome + $row2['totalIncome'];
  16. $tempAmount = $tempAmount + $row2['totalAmount'];
  17. $tempRowIndex = $tempRowIndex + 1;
  18. }
  19. echo "</table> <br>";

References

  1. Youtube - Run PHP with AMPPS in your local windows machine
  2. Book List sample xlsx
  3. Name List
  4. Surname List
  5. PDO Samples
  6. PHP Table Sample
  7. Procedure Sample
  8. Cursor Sample
  9. Official Turkey City List Download Link is kind of hidden on the page, so I want to put a screenshot :)