php
  1. php-select

PHP SELECT Statement

The SELECT statement is one of the most used statements in PHP, used for fetching data from a database table. It allows developers to retrieve and manipulate data from one or more tables in a database.

Syntax

The basic syntax for using the SELECT statement in PHP is as follows:

SELECT column_name(s) FROM table_name;

Here's an example of the SELECT statement in PHP:

<?php
// Database connection
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Select data from table
$sql = "SELECT id, name, email FROM users";
$result = $conn->query($sql);

// Display data
if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["id"]. " - Name: " . $row["name"]. " - Email: " . $row["email"]. "<br>";
    }
} else {
    echo "0 results";
}

// Close connection
$conn->close();
?>

Example

Suppose we have a database called "employees" with a table "users" that has the following data:

id name email
1 John Doe john.doe@example.com
2 Jane Smith jane.smith@example.com
3 Bob Jones bob.jones@example.com

To select all the columns from the "users" table using PHP, we can use the following code:

<?php
// Database connection
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "employees";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Select data from table
$sql = "SELECT * FROM users";
$result = $conn->query($sql);

// Display data
if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["id"]. " - Name: " . $row["name"]. " - Email: " . $row["email"]. "<br>";
    }
} else {
    echo "0 results";
}

// Close connection
$conn->close();
?>

Output

The output for the example above would be:

id: 1 - Name: John Doe - Email: john.doe@example.com
id: 2 - Name: Jane Smith - Email: jane.smith@example.com
id: 3 - Name: Bob Jones - Email: bob.jones@example.com

Explanation

  • We first create a connection to the database using the mysqli function.
  • Then, we create the SQL SELECT statement we want to execute, which in this case is SELECT * FROM users to select all columns from the "users" table.
  • We execute the $sql statement variable and store the results in the $result variable.
  • We then use the fetch_assoc() function to retrieve the rows of data returned by the SELECT statement. In the while loop, we iterate through the rows returned by the query and print out the column values (e.g. $row['id'], $row['name'], and $row['email']).
  • Finally, we close the database connection.

Use

The SELECT statement is used when we want to retrieve data from a database table. It can be used in a wide range of applications such as web applications that require user authentication, e-commerce websites that need to display product information, or blogs that fetch article content.

Important Points

Some important points to consider when using the SELECT statement in PHP are:

  • Always sanitize user input to avoid SQL injection attacks.
  • Use prepared statements when working with user input to help protect against SQL injection attacks.
  • Always close the database connection after completing database operations to avoid resource leaks.

Summary

In summary, the SELECT statement is an essential tool in PHP that allows developers to retrieve data from a database table. It is executed using the mysqli function and can be used to fetch data from one or more tables in a database. It is important to follow best practices when using the SELECT statement to securely and efficiently retrieve data from a database.

Published on: