-
API
Application Programming Interface, or API, defines the classes, methods, functions and variables that your application will need to call in order to carry out its desired task.
-
extension
term - extension. The PHP code consists of a core, with optional extensions to the core functionality.
-
MySQL-related extensions
PHP's MySQL-related extensions, such as the mysqli extension, and the mysql extension, are implemented using the PHP extension framework.
-
There are 3 main API options to connect to mysql.
- Mysqli extension
- Mysql extension:
- Php Data Objects (PDO)
-
Create object
- Create a mysqli object,
- connect to the MySQL server and open the database.
- Returns true on success and false on failure.
-
prepare
- protect against dangerous data reaching the database server.
- by separating the SQL from the user data and sending them separately to the server. (Malicious data would be a text field containing Bugs Bunny; drop table patients;)
- Prepared statements improve performance.
- prepares the SQL statement for execution.
- ? is used for user entered variables.
- returns a mysqli_stmt object on success
- returns false on failure.
-
bind_param method
- binds the specified values (user entered variables) to the ?s in the prepare statement.
- first parameter lists the data type of each ?
- s=string/date, i=int/bool, d=double, b=BLOB
- must be 1:1 ?s in prepare: variables in
- bind_param.
-
bind_result method
- binds the columns in the result set (the table data) to the specified variables.
- Returns true on success and
- false on failure.
-
execute
- The
- prepared statement is combined with the parameters values (if supplied) and statement is executed.
- Returns true on success,
- false on failure.
-
fetch
- Fetch or return the next available row from the query results and place the
- data in the bound variables.
- Prepared statements come as binary so
- data types are recognized for what they are.
- Returns false when all the rows have been returned.
- A record pointer is used to keep track
- of the current row.
-
affected_rows
Returns the number of rows that were added/changed in the last execute statement.
-
close
Close the prepared stmt object.
-
mysqli_close($mysqlObj)
Close the connection.
-
-
class
- A class is a definition of an object. A class contains data and the routines (methods) required to manipulate the data
- Defines attributes and methods of a particular type of object.
- It is a blue print for an object
-
object
- An instance of the class.
- Groups together data and routines in a data structure.
- You can create more than one object from a single class.
-
Attribute
- One piece of data inside the class.
- Also called property, class variable, data member.
- Attributes should be declared as private or protected, where possible.
-
Method
- A function inside a class.
- works on the attributes in the class to produce a result.
- works on only one object at a time.
- Also called property.
- Visibility can be:
- private: only accessible by the class that defines it.
- protected: accessible only within class itself and its inherited classes.
- public (the default): can be accessed anywhere.
- Functions cannot be overloaded.
- To call a method: $objectname->methodname(parameter list)
-
Constructor
- A method that is automatically called when the object is created.
- It is optional (but recommended) to add parameters to the constructor. These optional parameters can be used to assign initial values to the attributes when the object is created.
- If the constructor throws an exception on error, the object will not be created.
-
Destructor
- A method that is automatically executed when an object is destroyed.
- typically used to release resources.
- example, closing a database connection.
-
Coding Properties
- property is used to access private attributes from outside a class.
- A Get property returns the value of the attribute.
- A Set property receives a value as a parameter and assigns that value to the attribute.
- The parameter should be validated and only assigned to the attribute if valid.
- A public property can be directly accessed by code outside of the class.
- Private properties cannot be directly accessed by code outside the class.
- Protected properties can only be accessed within the class itself and its inherited classes.
- By default, properties are public.
- Style: provide the access modifier
- (private, protected or public)
- Within a class, variable $this is used to refer to the current object. It allows you to access the properties and methods of the current object.
- The object access operator (->) is used to provide the access.
|
|