Mysql
 sql >> Database >  >> RDS >> Mysql

Recupera i dati da mysql usando php

// Initializing connection data.
$host_db = 'localhost';
$name_db = 'article_db';
$user_db = 'username';
$pass_db = 'password';

try {
  // Connecting using the PDO object.
  $connection = new PDO("mysql:host=$host_db; dbname=$name_db", $user_db, $pass_db);

  // Setting the query and runnin it...
  $sql = "SELECT * FROM `article` WHERE `category` = 5 ORDER BY 3";
  $result = $connection->query($sql);

  // Iterating over the data and printing it.
  foreach($result as $row) {
      echo $row['id']. ' - '. $row['name']. ' - '. $row['category']. ' - '. $row['editor']. '<br />';
  }
  // Closing the connection.
  $connection = null;
}
// Catching it if something went wrong.
catch(PDOException $e) {
  echo $e->getMessage();
}