How can PHP be integrated into a mobile app for database access?
To integrate PHP into a mobile app for database access, you can create a web service using PHP that interacts with the database and returns data in a format like JSON or XML. The mobile app can then make HTTP requests to the PHP web service to retrieve or update data in the database.
<?php
// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Retrieve data from the database
$sql = "SELECT * FROM table";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// Output data in JSON format
$rows = array();
while($row = $result->fetch_assoc()) {
$rows[] = $row;
}
echo json_encode($rows);
} else {
echo "0 results";
}
$conn->close();
?>
Keywords
Related Questions
- Are there any best practices or recommendations for implementing a reload barrier in PHP to enhance security?
- What is the correct way to read and display specific parts of a JSON file using PHP?
- How can PHP be used to calculate and differentiate the need for CGI-enabled computers and regular computers in a school setting?