Chapter 16 - Review

Output
Show all Hide all
1Toggle answer visibilityWhat is a class? What is a method? What are variables defined within classes called?

A class is an encapsulated description of a type of data used in a programming language. It serves as a template for creating multiple objects of that class with inherited behaviors and characteristics. An instantiation of a class is an object. A method is a function within that class and a property is equivalent to a class variable.

2Toggle answer visibilityWhat is an object? How do you create an object in PHP? How do you call an object’s methods?

An object is an instance of a class. To create an object from an established class, you use the keyword new and the classname.
var myObject = new Classname();

To call a method on this object use the method name with any parameters.
myObject->doSomething(param1,param2);

3Toggle answer visibilityWhat is a constructor?

A constructor method is the function called when a class is instantiated as an object. It creates the object with it's inherited properties and methods.

4Toggle answer visibilityWhat is the syntax for creating a MySQLi object?

var connection = new MySQLi($host, $user, $pw, $db);

5Toggle answer visibility How do you execute any kind of query using MySQLi?

$connection->query("query_string");

6Toggle answer visibilityHow do you make string data safe to use in a query, when using MySQLi? Hint: there are two answers.

$query_string = $connection->real_escape_string($query_string);
OR use a prepared statement
$q="INSERT INTO tablename (this) VALUES (?)";
$stmt = $mysqli->prepare($q);
$stmt->bind_param("s", $param1);
$this = "string";
$stmt->execute( );

7Toggle answer visibility How do you check for, and display, a MySQLi error?

The MYSQLi object when created has an attribute called connect_error where any error is stored during instantiation.
echo $mysqli->connect_error;
If you are using PHP Version <5.3 you would need to use the procedural approach to check for connection errors...
if (mysqli_connect_error()) { echo mysqli_connect_error(); }

8Toggle answer visibilityHow do you fetch the results of SELECT queries using the MySQLi(and other) objects? What is the difference between using MySQLi_Result::fetch_array() and MySQLi_Result::fetch_object()?

Running a SELECT query on a mysqli object returns a MYSQLi_Result object. This has various methods for retrieving the data sets from the query. Use $result->fetch_array(_CONSTANT); to return a numeric array, associative array or both.
Use $result->fetch_object(); to return each row as an object that can be manipulated with that object's methods. When looping through each result set use the proper syntax for referring to either an ARRAY -- $row["columnName"] or $row[0] and an OBJECT $row->columnName.

9Toggle answer visibility How do you execute a prepared statement using the MySQLi and MySQLi_STMT classes?

First create your query string using placeholders for the values. Use the prepare method of the mysqli class which returns a mysqli_stmt object. Then use the bind method of the stmt class to assign the actual insertion values. Lastly - execute the stmt object.
$q="INSERT INTO tablename (this) VALUES (?)";
$stmt = $mysqli->prepare($q);
$stmt->bind_param("s", $param1);
$this = "string";
$stmt->execute( );

10Toggle answer visibilityWhat syntax is used to create a new DateTime object? What are the two ways you can set the object’s date and/or time?

$myDate = new DateTime();
The value of a DateTime object can be set during instatiation by passing a valid date, time value as the first argument. $xmas = new DateTime("2013-12-25"); or you can set it using the DateTime::setDate(yyy,mm,dd) method or setTime(h,m,s) method.

11Toggle answer visibility What is an exception?

An exception is an error thrown by an object.

Source
<?php
$review = array(
	1 => array(
		'q'=>'What is a class? What is a method? What are variables defined within classes called?',
		'a'=>'<p>A class is an encapsulated description of a type of data used in a programming language. It serves as a template for 
		creating multiple objects of that class with inherited behaviors and characteristics. An instantiation of a class is an object.
		 A method is a function within that class and a property is equivalent to a class variable.</p>'
	),
	2 => array(
		'q'=>'What is an object? How do you create an object in PHP? How do you call an object’s methods?',
		'a'=>'<p>An object is an instance of a class. To create an object from an established class, you use the keyword new and the classname. <br /> 
		<code>var myObject = new Classname();</code></p><p>To call a method on this object use the method name with any parameters.<br /> 
		<code>myObject->doSomething(param1,param2);</code></p>'
	),
	3 => array(
		'q'=>'What is a constructor?',
		'a'=>'<p>A constructor method is the function called when a class is instantiated as an object. It creates the object with 
		it\'s inherited properties and methods.</p>'
	),
	4 => array(
		'q'=>'What is the syntax for creating a MySQLi object?',
		'a'=>'<p><code>var connection = new MySQLi($host, $user, $pw, $db);</code></p>'
	),
	5 => array(
		'q'=>' How do you execute any kind of query using MySQLi?',
		'a'=>'<p><code>$connection->query("query_string");</code></p>'
	),
	6 => array(
		'q'=>'How do you make string data safe to use in a query, when using MySQLi? Hint: there are two answers.',
		'a'=>'<p><code>$query_string = $connection->real_escape_string($query_string);</code><br />OR use a prepared statement<br/>
		<code>$q="INSERT INTO tablename (this) VALUES (?)";<br/>$stmt = $mysqli->prepare($q);<br/>$stmt->bind_param("s", $param1);</br/>$this = "string";<br />$stmt->execute( );</code></p>'
	),
	7 => array(
		'q'=>' How do you check for, and display, a MySQLi error?',
		'a'=>'<p>The MYSQLi object when created has an attribute called connect_error where any error is stored during instantiation.
		<br /><code>echo $mysqli->connect_error;</code><br />
		If you are using PHP Version <5.3 you would need to use the procedural approach to check for connection errors... <br /><code>
		if (mysqli_connect_error()) {
			echo mysqli_connect_error();
		}</code></p>'
	),
	8 => array(
		'q'=>'How do you fetch the results of SELECT queries using the MySQLi(and other) objects? 
		What is the difference between using MySQLi_Result::fetch_array() and MySQLi_Result::fetch_object()?',
		'a'=>'<p>Running a SELECT query on a mysqli object returns a MYSQLi_Result object. This has various methods for 
		retrieving the data sets from the query. Use $result->fetch_array(_CONSTANT); to return a numeric array, associative array or both. </br />
		Use $result->fetch_object(); to return each row as an object that can be manipulated with that object\'s methods. When looping through
		each result set use the proper syntax for referring to either an ARRAY -- $row["columnName"] or $row[0] and an OBJECT $row->columnName.</p>'
	),
	9 => array(
		'q'=>' How do you execute a prepared statement using the MySQLi and MySQLi_STMT classes?',
		'a'=>'<p>First create your query string using placeholders for the values. Use the prepare method of the mysqli class which returns
		a mysqli_stmt object. Then use the bind method of the stmt class to assign the actual insertion values. Lastly - execute the stmt object.<br />
		<code>$q="INSERT INTO tablename (this) VALUES (?)";<br/>$stmt = $mysqli->prepare($q);<br/>$stmt->bind_param("s", $param1);</br/>$this = "string";<br />$stmt->execute( );</code></p>'
	),
	10 => array(
		'q'=>'What syntax is used to create a new DateTime object? What are the two ways you can set the object’s date 
		and/or time?',
		'a'=>'<p><code>$myDate = new DateTime();</code><br />The value of a DateTime object can be set during instatiation by passing
		a valid date, time value as the first argument. $xmas = new DateTime("2013-12-25"); or you can set it using the DateTime::setDate(yyy,mm,dd) method
		or setTime(h,m,s) method.</p>'
	),
	11 => array(
		'q'=>' What is an exception?',
		'a'=>'<p>An exception is an error thrown by an object.</p>'
	)
);
include('templates/review.php');