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
Q

What does mysql_select_db() do?

A

Indicates the database to be used for queries.

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

How would you add a new joke from a user, using php?

A

if (isset($_GET[‘addjoke’]))
{
include ‘form.html.php’;
exit()

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

When deleting a record, how will we identify which once we want removed?

A

By it’s ID

28
Q

Can vars in the main script be accessed or edited from an included file?

A

Yes. is can cause issues likes overwrites. Functions can protect you from this happening.

29
Q

What is a function’s scope?

A

Variables created inside a function exist only in that function

30
Q

Can vars from the mains script by accessed from within functions?

A

No.

31
Q

How can we mitigate issues that function and global variable scope can cause?

A

Keep track of var names and scripts

Keep track of which variables work within which scripts

32
Q

What are the two ways we can use vars in a function?

A

Importing a variable

using the $GLOBALS array

33
Q

What is the magic quotes function?

A
  • 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
Q

How do we import a variable to a function?

A

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
Q

If you edit an imported variable inside a function, is it affected outside the function?

A

Yes, the value is modified outside the function as well

36
Q

How do we use the $GLOBALS array?

A

Access with $GLOBALS[‘variable_name]

37
Q

What is the advantage to using the $GLOBALS array?

A

Keeps variables with a function scope separate

38
Q

Name some super global arrays

A
$_GET
$_POST
$_COOKIE
$_FILES
$_REQUEST
$_SERVER
$_SESSION
$_ENV
39
Q

Why do we have a stateful web?

A

Most programs preserve some state when they are closed.

There was a need for similar functionality in dynamic sites

40
Q

What does HTTP stand for?

A

HyperText Transfer Protocol

41
Q

What is HTTP responsible for?

A

Handling Requests

42
Q

What are the limitations of HTTP?

A

It is a stateless technology

Provides no user tracking

43
Q

What is the solution to the limitations of HTTP?

A

Cookies…and cream

44
Q

What is a Php cookie?

A

A name-value pair associated with a given website

45
Q

Where is a cookie stored?

A

On the client side and limited to 4kb of data

46
Q

Once the cookie is set, all following requests of the site will include what?

A

the cookie for that site

47
Q

When does a cookie expire?

A

When it becomes out of date or the browser is closed.

48
Q

Can cookies be accessed from any website?

A

Only the website that issued them

49
Q

How do we create a cookie?

A

Use the setcookie() function

50
Q

Do cookies behave the same way in all browsers?

A

No

51
Q

What is the one necessary parameter of a cookie?

A

The name

52
Q

What info will be kept int he cookie?

A

The value

The expiry time

53
Q

How would we set the cookie to expire in one hour

A

time() + 3600

54
Q

The setcookie() function’s domain allows you to:

A

Restrict the cookie’s access to a specific domain.

55
Q

How do we limit the cookie requests to only be sent over a secure connection?

A

Use ‘secure’

56
Q

httpOnly is used for what?

A

If set to 1, tells browser to prevent JavaSCript code on the site from seeing the cookie being sent

57
Q

What are the limitations of cookies?

A

Browsers limit the number and size of cookies per website

Browsers delete old cookies to make space for new ones

58
Q

What is the max size of a cookie?

A

4kb

59
Q

What do we use if we need more space than a cookie can provide?

A

A session

60
Q

What one value is stored in the browser to allow for a session?

A

The session ID, stored as a cookie.

61
Q

When a session is started, what does PHP generate?

A

PHP generates a randon session ID, a reference to that session, and its stored data

62
Q

Where is the value of the session set?

A

The special $_SESSION array

63
Q

What happens if an existing session ID is found when starting a session?

A

PHP restores the variables that belong to that session

64
Q

How do we set a session variable in the special $_SESSION array?

A

$ …_…SESSION… […’ name…’] = value

65
Q

How do we remove a variable from the current session?

A

unset SESSION name