db.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. <?php
  2. /**
  3. * Creates a database connection using the data in the config files.
  4. *
  5. * @param mode 'ro' for read-only, 'rw' for (limited) write privileges.
  6. * @return MySQL_connection or -1 on error or wrong mode.
  7. */
  8. function start_db($mode = 'ro'){
  9. global $auth;
  10. $db;
  11. //Connect to to database
  12. if ($mode == 'ro')
  13. $db = mysqli_connect($auth["host"], $auth["user"], $auth["pass"], $auth["name"]);
  14. else if ($mode == 'rw'){
  15. $db = mysqli_connect($auth["host"], $auth["user-rw"], $auth["pass-rw"], $auth["name"]);
  16. }
  17. else{
  18. return -1;
  19. }
  20. // Check connection
  21. if (mysqli_connect_errno()){
  22. error_log("Failed to connect to database: " . mysqli_connect_error());
  23. return -1;
  24. }
  25. //Set encoding options
  26. mysqli_set_charset($db, 'utf-8');
  27. header('Content-Type: text/html; charset=utf8');
  28. mysqli_query($db, 'SET NAMES utf8;');
  29. //Return the db connection
  30. return $db;
  31. }
  32. ?>