Posted By

snstro on 03/06/15


mysql select php data list connection pdo

Versions (?)

Who likes this?

1 person have marked this snippet as a favorite


Simple PDO Connection and select

 / Published in: PHP

This is the basic code that you need to know to connect and list data with PDO. Don't use de old method (mysqlconnect and mysqliconnect) to connect and interact with databases.

  1. <?php
  2. /* Connection string */
  3. $db = new PDO('mysql:host=localhost;dbname=YOUR_DB_NAME', 'root', 'YOUR_DB_PASSWORD', array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
  5. $stmt = $db->prepare('select * from your_table');
  6. $stmt->execute();
  8. // Get all records
  9. $result = $stmt->fetchAll();
  11. echo '<pre>';
  12. print_r($result); // Show a data array with results

Report this snippet  

You need to login to post a comment.