setting up database class in php OOP Flashcards
1
Q
MySQLi extension(the “i” stands for )
A
improved
2
Q
PDO Stands for
A
PHP DATA OBJCETS
3
Q
Mysqli and PDO difference
A
PDO will work on 12 different database systems, whereas MySQLi will only work with MySQL databases.
4
Q
Steps in creating the Class
A
- Create your file inside the classes folder or a like (the folder that will be called in class autoloading using SPL)
- Declare a private property/attributes of the following
Hostname
DB Username
DB Password
DB Name - Create a protected function e.g connect() or a user-defined method name.
- Instantiate a property named Data Source Name or dsn or any user-defined property name. (mysql:hostname=. +this ;dbname + this
- Instantiate a property named PDO as new PDO or any property defined by the user. Parameter of new PDO contains the Data Source Name, Class/this Property – DB Username and DB Password
- Set the Attribute of the PDO and call the PDO class directly using its default associated method e.g $pdo-> PDO::
ATTR_DEFAULT_FETCH_MODE
FETCH_ASSOC
Return the property name PDO
5
Q
ATTR_DEFAULT_FETCH_MODE
A
PDO::ATTR_DEFAULT_FETCH_MODE Fetches a row from a result set associated with a PDOStatement object. The mode parameter determines how PDO returns the row.
6
Q
FETCH_ASSOC
A
PDO::FETCH_ASSOC: returns an array indexed by column name as returned in your result set
7
Q
Creating a Class that Extends your DB class file (Displaying records from your Database)
A
- Create a new class file inside classes folder for SPL to auto detect it.
- Create a public function that will display the records in the database.
Instantiate a property name SQL or any user-defined property that will show Structured Query Language of Selecting the records from the DB Table.
Declare a property name or statement that will call the class’ connection method(from your db class) and will query the sql statement
Use looping statement e,g while ($row = statement->fetch())
Inside of your looping statement echo the fields you want to display
8
Q
Displaying Record by requirement category
A
- Create a public function that is parameterized by what you are requiring for Displaying purposes. (any user-defined method with properties inside parenthesis)
- Instantiate a property name SQL or any user-defined property that will show Structured Query Language of Selecting the records from the DB Table with a display condition.
- Declare a property name or statement that will call the class’ connection method(from your db class) and will prepare the sql statement
- Create a statement that will execute the parameterized properties
- Create a property name that will fetch the record based on the prepared statement e.g $row = $statement->fetchAll();
- Use a looping statement - this time use foreach e,g $row as $row
- Echo the $row[‘fields in your database’];
9
Q
Inserting New Record
A
- Create a public function that is parameterized by what you are requiring for Displaying purposes. (any user-defined method with properties inside parenthesis)
- Instantiate a property named SQL or any user-defined property that will show Structured Query Language of inserting records to the DB
- Declare a property name or statement that will call the class’ connection method(from your db class) and will prepare the sql statement
- Create a statement that will execute the parameterized properties