Part 5 - Server side Using databases with PHP Flashcards
(160 cards)
describe the fllowing property of a results object
num_rows
this results object property can be used to return the number of rows that are present within a results object
describe the PHP MySQLi method
select_db()
this PHP MySQLi function is used to select a database and connect our database object to it
the pitfall of these is that they only protect the column values of a statement and not the keywords or table names
this means that if we want dynamic selection of keywords and table names by the user then we need another method to ensure the data from the user is not malicious
what is a
**pitfall of prepared statements **
and what does this ultimately mean
Syntax:
INSERT INTO table (v1, v1, v1)
VALUES (v2, v2, v2)
Parameters:
table: specifies the name of the table that you want to insert new data into.
v1: specifies the columns of the table that you want to populate with new data.
v2: specifies the actual data that will be values within the new row.
Example:
INSERT INTO users (first_name, last_name, email) VALUES ('John', 'Doe', 'john.doe@example.com')
what is the syntax and params of the SQL statement
INSERT INTO
describe the method
query()
this method can be used to perform queries against a database
it Will return a result object if data is returned. Otherwise true or false depending on the success of the operation
what is the syntax and params of the method
prepare()
Syntax:
$mysqli -> prepare(query)
Param:
$mysqli - the database object/connection
Query - the sql query we wish to have prepared
We should consider this class of data as untrusted because it is possible that the data was not “cleaned” before it was stored.
If malicious JavaScript code was stored in the database and sent to the user, then a JavaScript attack could occur.
Therefore to make this data safe, we must escape any HTML output to the user to prevent this type of attack
Why should we consider
data from a database as untrusted
to achieve this in PHP, we can use the following code:
error_reporting(0); ini_set('display_errors', 0);
notes:
1. The error_reporting()
function sets which PHP errors are reported. The argument 0 turns off error reporting
2. The ini_set()
function is used to set values of PHP’s configuration settings. Passing the argument (‘display_errors’, 0) disables error reporting for this script.”
How do we
turn off all error reporting in PHP
name 2
benefits of data only existing once inside a relational database
benefits include:
1. it is more efficient (for database resources and structure)
2. it mitigates errors (an update only has to occur in one place for example)
describe the SQL statement
SHOW COLUMNS
This SQL statement can be used to gather column information from a table.
Information returned will be a table where each row represents data of the column such as its name and data type
example:
// Check the value of the 'sort' key in the $data array if ($data['sort'] == 'firstname') { // If the value is 'firstname', set the $sort variable to 'firstname' $sort = 'firstname'; } if ($data['sort'] == 'lastname') { // If the value is 'lastname', set the $sort variable to 'lastname' $sort = 'lastname'; } // Additional code goes here // Use the $sort and $order variables to dynamically specify the column and order in an SQL ORDER BY clause ORDER BY $sort $order
write an example of protecting against SQL injection when using dynamic column names or keywords from the user in an SQL statement
**During development **
where should errors be outputted?
during this time it may be easier to output errors to the web browser
describe the SQL clause
ORDER BY
This can be used to set the order of the output
we can specify which column should be used to define the sorting and whether it should be sorted ascending or descending
for this type of SQL statement we would expect the database to return an integer representing the number of affected rows
after executing an SQL DELETE statement what type of data could we expect returned from the database
using the SQL WHERE clause and the LIKE keyword write the statement that will
Will return any rows where the last_name column holds a value d anywhere in the entry
code xample:
WHERE last_name LIKE '%d%'
Syntax for SQL statement:
SELECT columns FROM table
Param
Columns - a comma separated list of columns we would like to select
Table - the table we would like to selct columns from
wha is the syntax and params of the SQL statement
SELECT
What values should we set in a script and use comparisons on the user’s data and our own values to protect against SQL injection attacks
this action should be performed on any user data that will dynamically change:
1. Database names
2. Table names
3. Column names
4. SQL keywords
this method can be used to perform queries against a database
it Will return a result object if data is returned. Otherwise true or false depending on the success of the operation
describe the method
query()
this is used so that we can prepare an SQL query for execution. This carries speed and security advantages.
On execution it will return A statement object on success. FALSE on failure
describe the PHP method
prepare()
describe the PHP method
bind_param()
this is used so that we can bind any variables we have to an already prepared statement
How can we
protect against SQL injection when using dynamic column names or keywords that are recieved from the user in an SQL statement?
We should NEVER use form data directly to populate column names or SQL keywords that we want to be dynamic. INSTEAD, we should use a comparison in the code between the form data and our expected values. If there is a match, we can explicitly set a variable ourselves that will be used to dynamically fill the SQL statement. This way, we can protect against SQL injection by ignoring any value that is not expected or proper
what is the syntax of the SQL statement
SHOW COLUMNS
syntax for SQL statement:SHOW COLUMNS FROM a_table
describe step 4 of creating prepared statements
Execute the prepared and bound statement
at this step we have a prepared and bound SQL statement and we are ready to execute it on the database.
Example:$stmt->execute();
this is the wild card symbol in SQL and is used with the LIKE keyword
LIKE ‘d%’ - would find any record that starts with the letter ‘d’
LIKE ‘%d’ - would find any record that ends with the letter ‘d’.
LIKE ‘%d%’ would find any record that has the letter ‘d’ in the value
describe the SQL
percentage (%) symbol
and three example usages
firstname: ' . $row['firstname'] . '
'; echo 'lastname: ' . $row['lastname'] . '
'; echo 'email: ' . $row['email'] . '
'; } ```row count: ' . $result->num_rows . '
';`firstname: ' . $row['firstname'] . '
'; echo 'lastname: ' . $row['lastname'] . '
'; echo 'email: ' . $row['email'] . '
'; } ```row count: ' . $result->num_rows . '
';`