PHP & MySQL · Lesson 6 of 11
SELECT
Read only the columns and rows an application needs.
Lesson
SELECT retrieves rows. Specify columns rather than using * in application queries, and add a deterministic ORDER BY when order matters. Paginate large result sets instead of loading an unbounded table into memory.
The example selects a small set of public profile fields in a stable order.
Further guidance
Use migrations to version schema changes, constraints to protect relationships, and prepared statements for every dynamic value. Scope writes with a precise condition, check affected rows, and use transactions when multiple statements must succeed together. Store credentials outside source control.
Practice task: Create a fresh example for SELECT. Predict the result before running it, then change one input and explain why the result changes. Test a normal case and one boundary or invalid case where it applies. Compare your output with the example and note one rule you would use in a real project.
Code examples
SELECT
SQL
SELECT id, display_name
FROM profiles
ORDER BY display_name ASC
LIMIT 20;
Expected output
At most 20 profile names in alphabetical order
Key points
- Read only the columns and rows an application needs.
- Use prepared statements for values and constraints for data integrity.
- Scope writes carefully and verify their result.
Your learning progress
Finish this lesson
Optional practice
PHP playground
This site does not execute learner-provided PHP code. Connect a secured PHP sandbox service before enabling code execution.
Execution is disabled. No code is sent to a server.