Chapter 9 - Review

Output
Show all Hide all
1Toggle answer visibilityWhat version of PHP are you using? What version of MySQL? Does your PHP-MySQL combination support the MySQL Improved extension?

Local Development Server
PHP v5.4.4
MySQL v5.5.25a
My system supports MySQLI

2Toggle answer visibilityWhat is the most important sequence of steps for debugging PHP-MySQL problems (explicitly covered at the end of Chapter 8, "Error Handling and Debugging"?

Troubleshooting errors in your PHP/MySQL script include these and MORE...

  • Make sure you are editing the proper script/page. With includes - lots of different files can contribute to the creation of a page. Know where you are.
  • Make sure any changes to the document have been saved.
  • Make sure you are accessing the file through a url.
  • Know which versions of key software are running and the limitations/functionality of that partcular version.
  • Determine which part of your script is faulty...USER error, HTML/CSS, Javascript/AJAX, PHP coding , MySQL formatting/queries, DATA in the database
  • Utilize all error reporting that comes built into the software. This can help with any PHP Parse errors, misspellings, syntactical mistakes, function parameter errors etc...
  • Use quotes methodically and consistantly.
  • Use comments to deactivate portions of code to narrow down the error.
  • Output to screen arrays, objects, and variables to track their expected values.
  • Use echo() to add comments within complex logic blocks to indicate code execution.
  • Print out mysql queries to see if any errors in expression.
  • Run mysql queries in client or separate ap (like Workbench) to test result set and query variations.
  • Double check database permissions and user information are compatible.
  • Step away from the machine.

3Toggle answer visibilityWhat hostname, username, and password combination do you, specifically, use to connect to MySQL?

For me to know and hackers to find out.

4Toggle answer visibilityWhat PHP code is used to connect to a MySQL server, select the database, and establish the encoding?

If using the MYSQLI Extension procedurally the separate steps would include:

		$handle = mysqli_connect($host,$user,$pw);
		mysqli_select_db($handle, $dbName);
		mysqli_set_charset($handle, 'utf8');
		

5Toggle answer visibilityWhat encoding are you using? Why is it necessary for the PHP scripts to use the same encoding that is used to interact with MySQL that is used for storing the text in the database?

The database for this book is using UTF8 as the default encoding. It is necessary to use the same encoding in our php scripts so data is saved consistently from the application to the database.

6Toggle answer visibilityWhy is it preferable to store the mysqli_connect.php script outside of the Web root directory? And what is the Web root directory?

Any sensitive files that should not be accessed via the web should be kept heirarchically above the root directory. These include files with passwords, usernames, key values that control your application etc. The root directory is the primary location from which your website files are stored and made public. It is the location that a browser will send a user when they request your domain.

7Toggle answer visibilityWhy shouldn't live sites show MySQL errors and the queries being run?

Live sites should NOT show mysql errors or queries because they allow anyone an insight into how your data is structured and what processes you are using to access your data. This information could be utilized by some one with bad intent to corrupt or damage your system.

8Toggle answer visibilityWhat syntax will you almost always use to handle the results of a SELECT query? What syntax could you use if the SELECT query returns only a single row?

A SELECT query usually requires looping through the returned records one row at a time. You would use a WHILE construct for this purpose.

while ($row = mysqli_fetch_array($r)) {
// Do something with $row.
}
If the result is known to only contain one row, you can just assign $row = mysqli_fetch_array($r) without the while loop.

9Toggle answer visibilityWhy is it important to use the mysqli_real_escape_string() function?

The mysqli_real_escape_string() returns a string that has had any problem characters that could interfere with the processing of your query prepended with a backslash. Characters encoded are NUL (ASCII 0), \n, \r, \, ', ", and Control-Z. This function also takes into account the current character set of the connection - so make sure it is set properly. Always sanitize before saving to the database, as user input can contain anything.

10Toggle answer visibilityAfter what kind of queries would you use the mysqli_num_rows() function?

The mysqli_num_rows() funtion is used with queries that return result objects to count how many rows are in the result. Primarily used with SELECT statements, but also SHOW, DESCRIBE or EXPLAIN. Use this function to test if there are any results to loop through before looping!

11Toggle answer visibilityAfter what types of queries would you use the mysqli_affected_rows() function?

The mysqli_affected_rows() function is used to determine how many rows were affected/changed by the query. It is generally used with INSERT, UPDATE, DELETE or REPLACE. Use this function to check that queries have completed the expected number of changes. Note - For SELECT statements mysqli_affected_rows() works like mysqli_num_rows().

Source
<?php
$review = array (
	// 1=>array(
	// 	'q'=>'',
	// 	'a'=>'<p></p>'
	// ),
	1=> array(
		'q'=>'What version of PHP are you using? What version of MySQL? Does your PHP-MySQL combination support the MySQL Improved extension?',
		'a' =>'<p><b>Local Development Server</b><br />PHP v5.4.4 <br />MySQL v5.5.25a<br />My system supports MySQLI</p>'
	),
	2=> array(
		'q'=>'What is the most important sequence of steps for debugging PHP-MySQL problems (explicitly covered at the end of Chapter 8,
		 "Error Handling and Debugging"?',
		'a'=>'<p>Troubleshooting errors in your PHP/MySQL script include these and MORE...
		<ul>
		<li>Make sure you are editing the proper script/page. With includes - lots of different files can contribute to the creation of a page. Know where you are.</li>
		<li>Make sure any changes to the document have been saved.</li>
		<li>Make sure you are accessing the file through a url.</li>
		<li>Know which versions of key software are running and the limitations/functionality of that partcular version.</li>
		<li>Determine which part of your script is faulty...USER error, HTML/CSS, Javascript/AJAX, PHP coding , MySQL formatting/queries, DATA in the database</li>
		<li>Utilize all error reporting that comes built into the software. This can help with any PHP Parse errors, misspellings, syntactical mistakes, function parameter errors etc...</li>
		<li>Use quotes methodically and consistantly.</li>
		<li>Use comments to deactivate portions of code to narrow down the error.</li>
		<li>Output to screen arrays, objects, and variables to track their expected values.</li>
		<li>Use echo() to add comments within complex logic blocks to indicate code execution.</li>
		<li>Print out mysql queries to see if any errors in expression.</li>
		<li>Run mysql queries in client or separate ap (like Workbench) to test result set and query variations.</li>
		<li>Double check database permissions and user information are compatible.</li>
		<li>Step away from the machine.</li>
		</ul>
		</p>'
	),
	3=> array(
		'q'=>'What hostname, username, and password combination do you, specifically, use to connect to MySQL?',
		'a'=>'<p>For me to know and hackers to find out.</p>'
	),
	4=> array(
		'q'=>'What PHP code is used to connect to a MySQL server, select the database, and establish the encoding?',
		'a'=>'<p>If using the MYSQLI Extension procedurally the separate steps would include: <br />
		<pre>
		$handle = mysqli_connect($host,$user,$pw);
		mysqli_select_db($handle, $dbName);
		mysqli_set_charset($handle, \'utf8\');
		</pre>
		</p>'
	),
	5=> array(
		'q'=>'What encoding are you using? Why is it necessary for the PHP scripts to use the same encoding that is used to interact with MySQL
		 that is used for storing the text in the database?',
		'a'=>'<p>The database for this book is using UTF8 as the default encoding. It is necessary to use the same encoding in our php scripts
		so data is saved consistently from the application to the database.</p>'
	),
	6=> array(
		'q'=>'Why is it preferable to store the mysqli_connect.php script outside of the Web root directory? And what is the Web root directory?',
		'a'=>'<p>Any sensitive files that should not be accessed via the web should be kept heirarchically above the root directory. These include files with
		passwords, usernames, key values that control your application etc. The root directory is the primary location from which your website
		 files are stored and made public. It is the location that a browser will send a user when they request your domain.</p>'
	),
	7=> array(
		'q'=>'Why shouldn\'t live sites show MySQL errors and the queries being run?',
		'a'=>'<p>Live sites should NOT show mysql errors or queries because they allow anyone an insight into how your data is structured and
		what processes you are using to access your data. This information could be utilized by some one with bad intent to corrupt or damage 
		your system.</p>'
	),
	8=> array(
		'q'=>'What syntax will you almost always use to handle the results of a SELECT query? What syntax could you use if the SELECT query
		 returns only a single row?',
		'a'=>'<p>A SELECT query usually requires looping through the returned records one row at a time. You would use a WHILE construct for 
		this purpose.
		<pre>while ($row = mysqli_fetch_array($r)) {
// Do something with $row.
}</pre> If the result is known to only contain one row, you can just assign $row = mysqli_fetch_array($r) without the while loop.</p>'
	),
	9=> array(
		'q'=>'Why is it important to use the mysqli_real_escape_string() function?',
		'a'=>'<p>The <b>mysqli_real_escape_string()</b> returns a string that has had any problem characters that could interfere with the processing of
		your query prepended with a backslash. Characters encoded are NUL (ASCII 0), \n, \r, \, \', ", and Control-Z.  This function also takes 
		into account the current character set of the connection - so make sure it is set properly. Always sanitize before saving to the 
		database, as user input can contain anything.</p>'
	),
	10=> array(
		'q'=>'After what kind of queries would you use the mysqli_num_rows() function?',
		'a'=>'<p>The <b>mysqli_num_rows()</b> funtion is used with queries that return result objects to count how many rows are in the result.
		 Primarily used with SELECT statements, but also SHOW, DESCRIBE or EXPLAIN. Use this function to test if there are any results to loop through
		 before looping!</p>'
	),
	11=> array(
		'q'=>'After what types of queries would you use the mysqli_affected_rows() function?',
		'a'=>'<p>The <b>mysqli_affected_rows()</b> function is used to determine how many rows were affected/changed by the query. It is 
		generally used with INSERT, UPDATE, DELETE or REPLACE. Use this function to check that queries have completed the expected number of changes.
		 Note - For SELECT statements mysqli_affected_rows() works like mysqli_num_rows().</p>'
	)
);
include('templates/review.php');
?>