db_utils.php 1.8 KB

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