Advertisement
suaveyy

DB_PASSWORD

Jul 11th, 2018
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.52 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.  
  69. // constructor
  70. function __construct($db_server, $db_user, $db_password, $db_database) {
  71.     $this->db_server = $db_server;
  72.     $this->db_user = $db_user;
  73.     $this->db_password = $db_password;
  74.     $this->db_database = $db_database;
  75.     // connecting to database
  76.     $this->connect();
  77. }
  78.  
  79. // destructor
  80. function __destruct() {
  81.     // closing db connection
  82.     $this->close();
  83. }
  84.  
  85. /**
  86.  * Function to connect with database
  87.  */
  88. function connect() {
  89.     if($this->con==NULL) {
  90.         // Connecting to mysql database
  91.         $this->con = mysqli_connect($this->db_server, $this->db_user, $this->db_password, $this->db_database) or die(mysqli_connect_error());
  92.     }
  93.  
  94.     // returing connection cursor
  95.     return $this->con;
  96. }
  97.  
  98. /**
  99.  * Function to close db connection
  100.  */
  101. function close() {
  102.     if($this->con!=NULL) {
  103.         // closing db connection
  104.         mysqli_close($this->con);
  105.     }
  106. }
  107.  
  108. }?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement