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
- What are the potential pitfalls of using relative paths in PHP, especially when dealing with safe mode restrictions?
- What resources or documentation should be consulted for further assistance in modifying the Amazon API script for different countries?
- What are the advantages of using $_POST and other superglobals over deprecated variables in PHP?