1+ <?php
2+
3+ // Connect to the database and execute a query.
4+ // whenever you have php file that only contains a class, it's common to name it the same as the class.
5+ // general conventions that says make first latter uppercase.
6+
7+ class Database
8+ {
9+ public $ connection ;
10+ public $ statement ;
11+
12+ public function __construct ($ config , $ username = 'root ' , $ password = 'password ' )
13+ {
14+
15+ // this is a function that takes an associative array and converts it to a query string.
16+ // http_build_query($data);// example.com?host=localhost&port=3306&dbname=testdb&charset=utf8mb4
17+ // dd('mysql:' . http_build_query($config, '', ';')); // host=localhost;port=3306;dbname=testdb;charset=utf8mb4
18+
19+ $ dsn = 'mysql: ' . http_build_query ($ config , '' , '; ' ); // host=localhost;port=3306;dbname=testdb;charset=utf8mb4
20+
21+ // This is automatically called when you create a new instance of the class.
22+ // both are same
23+ // $dsn = 'mysql:' . http_build_query($config, '', ';');
24+ // $dsn = "mysql:host={$config['host']};port={$config['port']};dbname={$config['dbname']};charset={$config['charset']}";
25+
26+ //$this for this instance of the class
27+ $ this ->connection = new PDO ($ dsn , $ username , $ password , [
28+ PDO ::ATTR_DEFAULT_FETCH_MODE => PDO ::FETCH_ASSOC
29+ ]);
30+ }
31+
32+ public function query ($ query , $ params = [])
33+ {
34+
35+ $ this ->statement = $ this ->connection ->prepare ($ query );
36+ $ this ->statement ->execute ($ params );
37+
38+ // return $statement;
39+ return $ this ;
40+ }
41+
42+ public function find ()
43+ {
44+ // $statement->fetch();
45+ return $ this ->statement ->fetch ();
46+ }
47+
48+ public function findOrFail ()
49+ {
50+ $ result = $ this ->find ();
51+
52+ if (! $ result ) {
53+ abort ();
54+ }
55+
56+ return $ result ;
57+ }
58+
59+ public function get ()
60+ {
61+ return $ this ->statement ->fetchAll ();
62+ }
63+ }
64+
65+ // $posts = $db->query('select * from posts');
66+ // foreach ($posts as $post) {
67+ // echo '<li>' . $post['title'] . '</li>';
68+ // }
69+
70+ // this class expect an argument
71+ // new PDO($dsn);
72+ // => dsn stands for Data Source Name
73+ // think of that as a connection string:a string that declares your connection to the database
74+
75+ // class Person
76+ // {
77+ // public $name;
78+ // public $age;
79+
80+ // // Functions with in a class are called methods.
81+ // // Technically you don't have to do public function breathe()
82+ // // because the default visibility is public.
83+ // // it's just common practice to be explicit.
84+ // // just follow practices like being explicit with visibility
85+
86+ // public function breathe()
87+ // {
88+ // echo $this->name . ' is breathing!';
89+ // }
90+ // }
91+
92+ // $person = new Person();
93+ // $person->name = 'John';
94+ // $person->age = 30;
95+
96+ // $person->breathe();
97+
98+ // return $statement->fetchAll(PDO::FETCH_ASSOC);
99+ // return $statement->fetch(PDO::FETCH_ASSOC);
100+ // $post = $db->query('select * from posts where id = 1')->fetch(PDO::FETCH_ASSOC);
101+ // $posts = $db->query('select * from posts')->fetchAll(PDO::FETCH_ASSOC);
0 commit comments