Executing queries with query method
📖 Scenario: You are building a simple PHP script to fetch user data from a database. You will practice running SQL queries using the query method.
🎯 Goal: Create a PHP script that connects to a database, runs a SELECT query using the query method, and displays the user names.
📋 What You'll Learn
Create a
mysqli connection object named $conn with host 'localhost', user 'root', password '', and database 'testdb'.Create a SQL query string
$sql to select all columns from the users table.Use the
query method on $conn with $sql to get the result in $result.Loop through
$result using fetch_assoc() and print each user's name.Close the database connection at the end.
💡 Why This Matters
🌍 Real World
Fetching data from a database is a common task in web development to show dynamic content like user profiles or product lists.
💼 Career
Understanding how to execute queries and handle results is essential for backend developers working with PHP and MySQL databases.
Progress0 / 4 steps