Mysql Select Data in PHP
To select data from a MySQL database using MySQLi in PHP, you can follow these steps:
Connect to the MySQL database.
Prepare and execute the SQL SELECT query.
Fetch and display the results.
Here’s a detailed example of how to do this:
Example 1: Select Data Using MySQLi (Object-Oriented)
<?php// Database connection parameters$host = "localhost";$username = "root";$password = "";$dbname = "testDB"; // Your database name// Create connection$conn = new mysqli($host, $username, $password, $dbname);// Check connectionif ($conn->connect_error) { die("Connection failed: " . $conn->connect_error);}// SQL query to select data$sql = "SELECT id, name, email FROM users"; // Example query to fetch id, name, and email from users table// Execute the query$result = $conn->query($sql);// Check if query was successful and fetch dataif ($result->num_rows > 0) { // Output data for each row while ($row = $result->fetch_assoc()) { echo "ID: " . $row["id"] . " - Name: " . $row["name"] . " - Email: " . $row["email"] . "<br>"; }} else { echo "No records found.";}// Close the connection$conn->close();?>Explanation:
$conn->query($sql): Executes the SQL query. TheSELECTstatement fetches the data from theuserstable.$result->num_rows > 0: Checks if any records were returned by the query.$row = $result->fetch_assoc(): Fetches the results row-by-row as an associative array, where column names are the keys and the column values are the values of the array.
Example 2: Select Data Using MySQLi (Procedural)
<?php// Database connection parameters$host = "localhost";$username = "root";$password = "";$dbname = "testDB"; // Your database name// Create connection$conn = mysqli_connect($host, $username, $password, $dbname);// Check connectionif (!$conn) { die("Connection failed: " . mysqli_connect_error());}// SQL query to select data$sql = "SELECT id, name, email FROM users";// Execute the query$result = mysqli_query($conn, $sql);// Check if query was successful and fetch dataif (mysqli_num_rows($result) > 0) { // Output data for each row while ($row = mysqli_fetch_assoc($result)) { echo "ID: " . $row["id"] . " - Name: " . $row["name"] . " - Email: " . $row["email"] . "<br>"; }} else { echo "No records found.";}// Close the connectionmysqli_close($conn);?>Explanation:
mysqli_connect($host, $username, $password, $dbname): Establishes the database connection.mysqli_query($conn, $sql): Executes the SQL query.mysqli_num_rows($result): Returns the number of rows in the result set.mysqli_fetch_assoc($result): Fetches the result row-by-row as an associative array.
Key Points:
Fetching Data: Use
fetch_assoc()to retrieve rows as associative arrays (column names as keys).Checking for Results: Always check if the result set contains data before processing it (
num_rows > 0).Closing the Connection: Close the connection to the database with
close()to free up resources.
This is the basic process of selecting data using MySQLi in PHP. Let me know if you need further assistance!