sudo apt-get google
ping -c 4 8.8.8.8
echo nameserver 8.8.8.8 > /etc/resolv.conf
class DB {
private $host;
private $user;
private $pass;
private $db;
private $connection;
public function __construct($host,$user,$pass,$db)
{
$this->host=$host;
$this->user=$user;
$this->pass=$pass;
$this->db=$db;
}
public function connect()
{
$opt = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
);
$dsn = "mysql:host={$this->host};dbname={$this->db}";
try {
$this->connection = new PDO($dsn, $this->user, $this->pass, $opt);
} catch (PDOException $e) {
die( "Хьюстон, у нас проблемы. Бегите за шапманским, будем плакать вместе! Ёлочка вам нравится?" );
echo $e->getMessage();
}
$this->connection->exec("SET NAMES 'utf8'");
$this->connection->exec("SET CHARACTER SET 'utf8'");
$this->connection->exec("SET SESSION collation_connection = 'utf8_general_ci'");
return $this->connection;
}
}
class News {
private $connection;
public function __construct($connection)
{
$this->connection=$connection;
}
public function get_news()
{
$result= $this->connection->query('SELECT * FROM news');
return $result->fetchAll(PDO::FETCH_UNIQUE);
}
}
$db=new DB('host', 'un', 'pass', 'db');
$connection=$db->connect();
$news=new News($connection);
$news_list=$news->get_news();
var_dump($news_list);