I am making an application, I have a database in Hostinger already created and the PHP files also connected so that I can take the corresponding columns. My problem is that I do not know how to connect these PHP with Java . How do I make the connection? What I want is to simply show the query, it is a test application.
db_connector.php
<?php
define('DB_USER', "****"); // db user
define('DB_PASSWORD', "****"); // db password (mention your db password here)
define('DB_DATABASE', "****"); // database name
define('DB_SERVER', "mysql.hostinger.es"); // db server?>
db_config.php
<?php
// array for JSON response
$response = array();
// include db connect class
require_once __DIR__ . '/db_connect.php';
// import database connection variables
require_once __DIR__ . '/db_config.php';
// connecting to db
$db = new DB_CONNECT(DB_SERVER, DB_USER, DB_PASSWORD, DB_DATABASE);
// get all columns from Preguntes table
$result = mysqli_query($db->connect(),"SELECT * FROM Preguntes") or die(mysql_error());
// check for empty result
if (mysqli_num_rows($result) > 0) {
// looping through all results
$response["test"] = array();
while ($row = mysqli_fetch_array($result)) {
// temp user array
$test = array();
$test["ID"] = $row["ID"];
$test["Pregunta"] = $row["Pregunta"];
$test["Respostes"] = $row["Respostes"];
$test["Image"] = $row["Image"];
// push test into final response array
array_push($response["test"], $test);
}
// success
$response["success"] = 1;
// echoing JSON response
echo json_encode($response);
} else {
// query is empty
$response["success"] = 0;
$response["message"] = "Query is empty";
// echo no query JSON
echo json_encode($response);
}?>
database.php
/**
* A class file to connect to database
*/
require_once 'db_config.php';
class DB_CONNECT {
// connexion property
private $con = NULL;
private $db_server;
private $db_user;
private $db_password;
private $db_database;
// constructor
function __construct($db_server, $db_user, $db_password, $db_database) {
$this->db_server = $db_server;
$this->db_user = $db_user;
$this->db_password = $db_password;
$this->db_database = $db_database;
// connecting to database
$this->connect();
}
// destructor
function __destruct() {
// closing db connection
$this->close();
}
/**
* Function to connect with database
*/
function connect() {
if($this->con==NULL) {
// Connecting to mysql database
$this->con = mysqli_connect($this->db_server, $this->db_user, $this->db_password, $this->db_database) or die(mysqli_connect_error());
}
// returing connection cursor
return $this->con;
}
/**
* Function to close db connection
*/
function close() {
if($this->con!=NULL) {
// closing db connection
mysqli_close($this->con);
}
}
}?>