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

  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.

CodeCapsule

Sanjit Sinha — Web Developer | PHP • Laravel • CodeIgniter • MySQL • Bootstrap Founder, CodeCapsule — Student projects & practical coding guides. Email: info@codecapsule.in • Website: CodeCapsule.in

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *