db_utils.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. // Connect to the MySQL database
  3. function db_connect() {
  4. $database = mysqli_connect("localhost", "mysqluser", "mysqlpassword", "ethicscanvas");
  5. if(!$database) {
  6. throw new Exception(mysqli_connect_errno() . ": " . mysqli_connect_error());
  7. }
  8. return $database;
  9. /*
  10. include('config.php');
  11. if(isset($db_host, $db_username, $db_password, $db_name)) {
  12. // Create connection to database
  13. $conn = new mysqli($db_host, $db_username, $db_password, $db_name);
  14. if ($conn->connect_error) {
  15. echo 4004; // Unsuccessful connection to database
  16. }
  17. }
  18. else { // Connection variables not set
  19. echo 4005; // Respond with bad request code
  20. }
  21. return $conn;
  22. */
  23. }
  24. // Closes the connection to the database
  25. function db_close($conn) {
  26. $conn->close();
  27. }
  28. // Run query with a return
  29. function db_query_return($inDatabase, $inQuery) {
  30. if(!$recordSet = mysqli_query($inDatabase, $inQuery)) {
  31. throw new Exception(mysqli_errno($inDatabase) . ": " . mysqli_error($inDatabase));
  32. }
  33. return $recordSet;
  34. }
  35. // Run query without any return
  36. function db_query_no_return($inDatabase, $inQuery) {
  37. if(!mysqli_query($inDatabase, $inQuery)) {
  38. throw new Exception(mysqli_errno($inDatabase) . ": " . mysqli_error($inDatabase));
  39. }
  40. }
  41. ?>