PHP MySQL Select Data

PHP Tutorial

PHP MySQL Select Data Tutorial

In PHP MySQL Select Data allows you to retrieve and display records in your PHP applications. You can use mysqli or PDO for this purpose.


 Using mysqli (Procedural)


 

  • mysqli_fetch_assoc() fetches each row as an associative array.

 Using mysqli Object-Oriented


 

  • $result->fetch_assoc() retrieves each row as an associative array.

 Using PDO


 

  • fetchAll(PDO::FETCH_ASSOC) retrieves all rows as an associative array.

Using Prepared Statements for Select (Secure)


 

  •  Prepared statements protect against SQL injection.

 Key Points

  1. Use mysqli_fetch_assoc() or fetch_assoc() for associative arrays.

  2. Use PDO fetch modes for flexible data retrieval:

    • PDO::FETCH_ASSOC → associative array

    • PDO::FETCH_NUM → numeric array

    • PDO::FETCH_OBJ → object

  3. Prepared statements should be used for user-supplied input.

  4. Always close connections and statements after use.

You may also like...