class Database extends PDO{
public function __construct() {
$server = 'Crazy\INTz';
$dbname = 'mvc';
$user = 'sa';
$password = '12345678';
parent::__construct('mssql:host='.$server.';'.'dbname='.$dbname
,$user
,$password);
echo "Connected !";
}
}
เรียกใช้โดย Model
Code (PHP)
class Model {
public function __construct() {
$this->database = new Database();
}
}
รับคำสั่งจากหน้า login
Code (PHP)
class Login extends Controller{
function __construct() {
parent::__construct();
}
function index(){
$this->view->render('login/index');
}
function run(){
$this->model->run();
}
}
และแสดงผลที่หน้านี้
Code (PHP)
class Login_Model extends Model{
public function __construct() {
parent::__construct();
}
public function run(){
$sth = $this->db->prepare("SELECT ID FROM Users WHERE Login = :login And Password = :password ");
$sth->execute(array(
':login' => $_POST['login'],
':password' => $_POST['password']
));
$data = $sth->fetchAll();
print_r($data);
}
}
class Model {
public function __construct() {
$this->database = new Database();
}
}
Code
class Login_Model extends Model{
public function __construct() {
parent::__construct();
}
public function run(){
$sth = $this->db->prepare("SELECT ID FROM Users WHERE Login = :login And Password = :password ");
$sth->execute(array(
':login' => $_POST['login'],
':password' => $_POST['password']
));
$data = $sth->fetchAll();
print_r($data);
}
}