Advertisement
Guest User

Untitled

a guest
Jan 21st, 2017
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. <?php
  2. define('DB_USER', "****"); // db user
  3. define('DB_PASSWORD', "****"); // db password (mention your db password here)
  4. define('DB_DATABASE', "****"); // database name
  5. define('DB_SERVER', "mysql.hostinger.es"); // db server?>
  6.  
  7. <?php
  8.  
  9. // array for JSON response
  10. $response = array();
  11.  
  12. // include db connect class
  13. require_once __DIR__ . '/db_connect.php';
  14. // import database connection variables
  15. require_once __DIR__ . '/db_config.php';
  16.  
  17. // connecting to db
  18. $db = new DB_CONNECT(DB_SERVER, DB_USER, DB_PASSWORD, DB_DATABASE);
  19.  
  20. // get all columns from Preguntes table
  21. $result = mysqli_query($db->connect(),"SELECT * FROM Preguntes") or die(mysql_error());
  22.  
  23. // check for empty result
  24. if (mysqli_num_rows($result) > 0) {
  25. // looping through all results
  26.  
  27. $response["test"] = array();
  28.  
  29. while ($row = mysqli_fetch_array($result)) {
  30. // temp user array
  31. $test = array();
  32. $test["ID"] = $row["ID"];
  33. $test["Pregunta"] = $row["Pregunta"];
  34. $test["Respostes"] = $row["Respostes"];
  35. $test["Image"] = $row["Image"];
  36.  
  37. // push test into final response array
  38. array_push($response["test"], $test);
  39. }
  40. // success
  41. $response["success"] = 1;
  42.  
  43. // echoing JSON response
  44. echo json_encode($response);
  45. } else {
  46. // query is empty
  47. $response["success"] = 0;
  48. $response["message"] = "Query is empty";
  49.  
  50. // echo no query JSON
  51. echo json_encode($response);
  52. }?>
  53.  
  54. /**
  55. * A class file to connect to database
  56. */
  57.  
  58. require_once 'db_config.php';
  59. class DB_CONNECT {
  60.  
  61. // connexion property
  62. private $con = NULL;
  63. private $db_server;
  64. private $db_user;
  65. private $db_password;
  66. private $db_database;
  67.  
  68. // constructor
  69. function __construct($db_server, $db_user, $db_password, $db_database) {
  70. $this->db_server = $db_server;
  71. $this->db_user = $db_user;
  72. $this->db_password = $db_password;
  73. $this->db_database = $db_database;
  74. // connecting to database
  75. $this->connect();
  76. }
  77. // destructor
  78. function __destruct() {
  79. // closing db connection
  80. $this->close();
  81. }
  82. /**
  83. * Function to connect with database
  84. */
  85. function connect() {
  86. if($this->con==NULL) {
  87. // Connecting to mysql database
  88. $this->con = mysqli_connect($this->db_server, $this->db_user, $this->db_password, $this->db_database) or die(mysqli_connect_error());
  89. }
  90. // returing connection cursor
  91. return $this->con;
  92. }
  93. /**
  94. * Function to close db connection
  95. */
  96. function close() {
  97. if($this->con!=NULL) {
  98. // closing db connection
  99. mysqli_close($this->con);
  100. }
  101. }
  102. }?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement