PHP MySQL Select Data
📄 PHP MySQL Select Data Tutorial
Selecting data from a MySQL database allows you to retrieve and display records in your PHP applications. You can use mysqli or PDO for this purpose.
1️⃣ Using mysqli (Procedural)
✅ mysqli_fetch_assoc() fetches each row as an associative array.
2️⃣ Using mysqli Object-Oriented
✅ $result->fetch_assoc() retrieves each row as an associative array.
3️⃣ Using PDO
✅ fetchAll(PDO::FETCH_ASSOC) retrieves all rows as an associative array.
4️⃣ Using Prepared Statements for Select (Secure)
✅ Prepared statements protect against SQL injection.
5️⃣ Key Points
-
Use
mysqli_fetch_assoc()orfetch_assoc()for associative arrays. -
Use PDO fetch modes for flexible data retrieval:
-
PDO::FETCH_ASSOC→ associative array -
PDO::FETCH_NUM→ numeric array -
PDO::FETCH_OBJ→ object
-
-
Prepared statements should be used for user-supplied input.
-
Always close connections and statements after use.
