db.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. /**
  3. * Creates a database connection using the data in the config files.
  4. *
  5. * @return MySQL_connection Connection to the database.
  6. */
  7. function start_db($db_configuration){
  8. $type = $db_configuration["type"];
  9. $host = $db_configuration["host"];
  10. $port = $db_configuration["port"];
  11. $name = $db_configuration["name"];
  12. $user = $db_configuration["user"];
  13. $pass = $db_configuration["pass"];
  14. $dbHost="localhost";
  15. $dbName="myDB";
  16. $dbUser="root"; //by default root is user name.
  17. $dbPassword=""; //password is blank by default
  18. try{
  19. $dbConn= new PDO("mysql:host=$dbHost;dbname=$dbName",$dbUser,$dbPassword);
  20. Echo "Successfully connected with myDB database";
  21. } catch(Exception $e){
  22. Echo "Connection failed" . $e->getMessage();
  23. }
  24. // TODO: Types and port
  25. $db = mysqli_connect($auth["host"], $auth["user"], $auth["pass"], $auth["name"]);
  26. // Check connection
  27. if (mysqli_connect_errno()){
  28. error_log("Failed to connect to database: " . mysqli_connect_error());
  29. return -1;
  30. }
  31. //Set encoding options
  32. mysqli_set_charset($db, "utf-8");
  33. mysqli_query($db, "SET NAMES utf8;");
  34. return $db;
  35. }
  36. ?>