Lecture 9 - Stateful Web Flashcards

1
Q

What does PDO stand for?

A

PHP Data Object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

To configure the PDO to throw an exception any time there’s a failure?

A

Use the PDO’s setAttribute() method

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the exec() method used for?

A

Queries can be sent to a MYSQL database using it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does PDO do?

A

Identifies that the connection to a database has been established

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

If PHP cannot handle PDO, what will it do?

A

Throws a php exception

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does the try-catch statement do?

A
try {
do something
}
catch (Exception type $e){
handle exception
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does this code do?

A

This function returns a PHP Data Object (PDO)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

IF this PDO code can’t be performed, what happens?

A

PHP will throw an exception

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does PHP do with a database once a connection is no longer needed?

A

It automatically disconnects

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you force PHP to close a connection?

A

$pdo = null

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you connect to a database with procedural php?

A

$variable_name = mysql_connect(hostname, username, password);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you close a connection to a mysql database with procedural php?

A

mysql_close();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What must be do before answering a query?

A

Establish a database connection

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are the best practices of querying a database?

A

Establish a connection at the start of the php script

During development, leave clues in case of issues when passing queries to the server. (relevant error messages)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Name three sql queries

A

DELETE, INSERT, UPDATE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

PDO Method: How do we create a table?

A

$sql = ‘CREATE TABLE jokes(id INT NOT NULLAUTO_INCREMENT PRIMARY KEY,
joketext TEXT,
jokedate DATE NOT NULL,
)

$pdo->exec($sql)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

PDO Method: How do we update a table?

A

$sql = ‘UPDATE joke SET jokedate = “2012” WHERE joke text LIKE “%chicken%”;

$affectedRows = $pdo ->exec($sql)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

PDO Method: How do we SELECT?

A
$sql = 'SELECT * FROM tablename'; 
$result = $pdo->query($sql);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What makes SELECT queries different from other queries?

A

They produce results

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Procedural Method: How do we send SQL queries through php?

A

$variable_name = mysqli_query(sql statement);

Example:
$variable_name = mysqli_query(SELECT*FROM customers);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What would the result set of a query contain?

A

A list of all the entries returned from the query

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Which PHP functions are needed to connect to the database using procedural php?

A

mysqli_connect()
mysql_set_chart()
mysqli_select_db()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What does mysqli_connect() do?

A

Connects to the database

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

What does mysql_set_chartset() do?

A

Indicates the character set to be used for
communications between PHP and the DB

Must match the character set used by HTML pages,
DB and its tables

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
What does mysql_select_db() do?
Indicates the database to be used for queries.
26
How would you add a new joke from a user, using php?
if (isset($_GET['addjoke'])) { include 'form.html.php'; exit()
27
When deleting a record, how will we identify which once we want removed?
By it's ID
28
Can vars in the main script be accessed or edited from an included file?
Yes. is can cause issues likes overwrites. Functions can protect you from this happening.
29
What is a function's scope?
Variables created inside a function exist only in that function
30
Can vars from the mains script by accessed from within functions?
No.
31
How can we mitigate issues that function and global variable scope can cause?
Keep track of var names and scripts Keep track of which variables work within which scripts
32
What are the two ways we can use vars in a function?
Importing a variable using the $GLOBALS array
33
What is the magic quotes function?
- Causes as many issues as it solves - Only works in some circumstances - Functionality depends on site's character encoding - Adds backslashes to escape characters expected in a sql query, even if it's just plain text - We still need to work with it for now
34
How do we import a variable to a function?
Using the global statement you can import more than one variable. If you modify an imported variable inside the function, it's value is modified outside the function.
35
If you edit an imported variable inside a function, is it affected outside the function?
Yes, the value is modified outside the function as well
36
How do we use the $GLOBALS array?
Access with $GLOBALS['variable_name]
37
What is the advantage to using the $GLOBALS array?
Keeps variables with a function scope separate
38
Name some super global arrays
``` $_GET $_POST $_COOKIE $_FILES $_REQUEST $_SERVER $_SESSION $_ENV ```
39
Why do we have a stateful web?
Most programs preserve some state when they are closed. There was a need for similar functionality in dynamic sites
40
What does HTTP stand for?
HyperText Transfer Protocol
41
What is HTTP responsible for?
Handling Requests
42
What are the limitations of HTTP?
It is a stateless technology Provides no user tracking
43
What is the solution to the limitations of HTTP?
Cookies...and cream
44
What is a Php cookie?
A name-value pair associated with a given website
45
Where is a cookie stored?
On the client side and limited to 4kb of data
46
Once the cookie is set, all following requests of the site will include what?
the cookie for that site
47
When does a cookie expire?
When it becomes out of date or the browser is closed.
48
Can cookies be accessed from any website?
Only the website that issued them
49
How do we create a cookie?
Use the setcookie() function
50
Do cookies behave the same way in all browsers?
No
51
What is the one necessary parameter of a cookie?
The name
52
What info will be kept int he cookie?
The value The expiry time
53
How would we set the cookie to expire in one hour
time() + 3600
54
The setcookie() function's domain allows you to:
Restrict the cookie's access to a specific domain.
55
How do we limit the cookie requests to only be sent over a secure connection?
Use 'secure'
56
httpOnly is used for what?
If set to 1, tells browser to prevent JavaSCript code on the site from seeing the cookie being sent
57
What are the limitations of cookies?
Browsers limit the number and size of cookies per website Browsers delete old cookies to make space for new ones
58
What is the max size of a cookie?
4kb
59
What do we use if we need more space than a cookie can provide?
A session
60
What one value is stored in the browser to allow for a session?
The session ID, stored as a cookie.
61
When a session is started, what does PHP generate?
PHP generates a randon session ID, a reference to that session, and its stored data
62
Where is the value of the session set?
The special $_SESSION array
63
What happens if an existing session ID is found when starting a session?
PHP restores the variables that belong to that session
64
How do we set a session variable in the special $_SESSION array?
$ ..._...SESSION... [...' name...'] = value
65
How do we remove a variable from the current session?
unset SESSION name