How to Get All Data of Particular ID and Display using Core PHP 8

Hello Friends, In this tutorial we will learn how to retrieve data from a database using PHP 8, you can follow these general steps:

  1. Connect to the database using the mysqli_connect() function.
  2. Write and execute an SQL query to retrieve the data you need.
  3. Fetch the data from the result set and store it in a PHP variable.
  4. Close the database connection.

Here's an example of how to retrieve data of a particular ID and display it using core PHP version 8:

// Step 1: Connect to the database
$servername = "localhost";
$username = "your_username";
$password = "your_password";
$dbname = "your_database_name";

$conn = mysqli_connect($servername, $username, $password, $dbname);

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());

// Step 2: Write and execute the SQL query
$id = 123; // replace with the ID you want to retrieve
$sql = "SELECT * FROM your_table_name WHERE id = $id";
$result = mysqli_query($conn, $sql);

// Step 3: Fetch the data and store it in a PHP variable
if (mysqli_num_rows($result) > 0) {
    $row = mysqli_fetch_assoc($result);
    // you can access the data using $row['column_name']
    echo "ID: " . $row['id'] . "<br>";
    echo "Name: " . $row['name'] . "<br>";
    echo "Email: " . $row['email'] . "<br>";
    // add more columns as needed
} else {
    echo "0 results";


// Step 4: Close the database connection

In this example, we first connect to the database using mysqli_connect(). Then, we write an SQL query to retrieve the data of the ID we want. We use mysqli_query() to execute the query and store the result set in $result.

Next, we check if there are any rows in the result set using mysqli_num_rows(). If there are, we fetch the data using mysqli_fetch_assoc() and store it in $row. We can then access the data using $row['column_name'].

Finally, we close the database connection using mysqli_close().