What are some common tools for executing SQL commands for MySQL databases, specifically for PHP beginners?
When working with MySQL databases in PHP, beginners can use tools such as phpMyAdmin, MySQL Workbench, or command-line interfaces like MySQL CLI or Sequel Pro to execute SQL commands. These tools provide user-friendly interfaces for interacting with the database, running queries, and managing data efficiently.
<?php
// Example of executing SQL command in PHP using mysqli extension
// Connect to 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);
}
// SQL query to retrieve data from a table
$sql = "SELECT * FROM table_name";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// Output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["name"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>