SQL Tutorial
SQL Clauses / Operators
SQL-Injection
SQL Functions
SQL Queries
PL/SQL
MySQL
SQL Server
Misc
In MySQL, the SELECT
statement is used to fetch data from one or more tables. When working with PHP, you can use the MySQLi (or PDO) extension to execute a SELECT
query and retrieve data.
Here's how you can execute a SELECT
query in PHP using the MySQLi extension:
$servername = "your_server_name"; $username = "your_username"; $password = "your_password"; $dbname = "your_database_name"; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); }
Assuming you have a table named users
and you want to retrieve all its records:
$sql = "SELECT id, name, email FROM users"; $result = $conn->query($sql); if ($result->num_rows > 0) { // Fetch each row as an associative array and display the results while($row = $result->fetch_assoc()) { echo "ID: " . $row["id"] . " - Name: " . $row["name"] . " - Email: " . $row["email"] . "<br>"; } } else { echo "0 results"; }
$conn->close();
Some Tips:
SELECT
statement instead of *
unless you need all columns. This makes the query more efficient.Using Prepared Statements:
If you want a more secure approach, especially when handling user input, consider using prepared statements. They help prevent SQL injection attacks:
// Example using prepared statements with MySQLi to fetch users with a specific ID $stmt = $conn->prepare("SELECT id, name, email FROM users WHERE id = ?"); $stmt->bind_param("i", $userID); // "i" means integer $userID = 5; // or get this from user input $stmt->execute(); $result = $stmt->get_result(); while ($row = $result->fetch_assoc()) { echo "ID: " . $row["id"] . " - Name: " . $row["name"] . " - Email: " . $row["email"] . "<br>"; } $stmt->close(); $conn->close();
In the example above, we used the prepare
method to prepare an SQL statement with a placeholder (?
). The bind_param
method is then used to bind the placeholder to a specific value, with "i" indicating that we're binding an integer.
How to retrieve data from MySQL using PHP:
<?php $conn = new mysqli("localhost", "your_username", "your_password", "your_database"); if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $sql = "SELECT * FROM your_table"; $result = $conn->query($sql); // Process the result set... $conn->close(); ?>
Retrieving specific columns with SELECT in PHP:
$sql = "SELECT column1, column2 FROM your_table";
Filtering data with WHERE clause in PHP and MySQL:
$condition = "value"; $sql = "SELECT * FROM your_table WHERE column_name = '$condition'";
PHP MySQL SELECT DISTINCT values:
$sql = "SELECT DISTINCT column_name FROM your_table";
ORDER BY clause in SELECT query with PHP:
$sql = "SELECT * FROM your_table ORDER BY column_name";
Using LIMIT clause for result pagination in PHP:
$page = $_GET['page'] ?? 1; $limit = 10; $offset = ($page - 1) * $limit; $sql = "SELECT * FROM your_table LIMIT $limit OFFSET $offset";
Selecting records based on conditions in PHP:
$condition1 = "value1"; $condition2 = "value2"; $sql = "SELECT * FROM your_table WHERE column1 = '$condition1' AND column2 = '$condition2'";
JOIN operations in SELECT query with PHP and MySQL:
$sql = "SELECT * FROM table1 JOIN table2 ON table1.id = table2.id";
GROUP BY clause in PHP MySQL SELECT:
$sql = "SELECT column_name, COUNT(*) FROM your_table GROUP BY column_name";
Aggregating data with COUNT, SUM, AVG in PHP SELECT:
$sql_count = "SELECT COUNT(*) FROM your_table"; $sql_sum = "SELECT SUM(column_name) FROM your_table"; $sql_avg = "SELECT AVG(column_name) FROM your_table";
Handling NULL values in SELECT query with PHP:
$sql = "SELECT * FROM your_table WHERE column_name IS NULL";
Selecting records based on user input in PHP:
$user_input = $_POST['user_input']; $sql = "SELECT * FROM your_table WHERE column_name = '$user_input'";
PHP MySQL SELECT query error handling:
if ($result = $conn->query($sql)) { // Process the result set... } else { echo "Error: " . $conn->error; }