How can PHP be integrated with MySQL for database management?
To integrate PHP with MySQL for database management, you can use the MySQLi or PDO extension in PHP. These extensions allow you to connect to a MySQL database, execute queries, and retrieve data from the database using PHP scripts.
// Create a connection to the MySQL database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database_name";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Perform a query
$sql = "SELECT * FROM table_name";
$result = $conn->query($sql);
// Fetch data from the result set
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["name"]. "<br>";
}
} else {
echo "0 results";
}
// Close the connection
$conn->close();
Keywords
Related Questions
- What steps should be taken to correctly configure the session save path in the php.ini file for PHP applications running on a Windows server?
- What is the recommended approach for loading content into specific sections of a webpage using PHP?
- What are the limitations of PHP in terms of sorting data based on multiple criteria and how can they be overcome?