Learning PHP and MySQL
Thursday, July 06, 2006
Welcome to the O’Reilly Learning PHP & MySQL blog
Here you will find code directly from the book, error listings, code fixes, and trivia about the computing world. Welcome. The authors of Learning PHP & MySQL Michele Davis and Jon Phillips will also be happy to answer any questions. So please use your real name when signing in! We look forward to a lively blog with lots of comments!
Source code for each chapter is posted under the catagories on the right side bar.
Brett Merkey Book Review
This review was taken from amazon.com
The Very Book for the Very Beginner, June 29, 2006
Reviewer: Brett Merkey (Palm Harbor, FL United States)
This book will take you from a basic understanding of creating static HTML to an elementary but quite nifty practical knowledge of serving up dynamic Web pages on your own.
Chapters 1-6 take you through basic orientation, the installation of Apache, PHP and MySQL, followed by an intro to PHP statements.
Chapters 7-9 introduce database concepts and step you through getting PHP to talk to MySQL.
Chapters 10-17 begin the process of creating forms and other components of Web sites and applications following all the way through to integrating some sample applications.
Each step and procedure has ample code printouts, logic diagrams and output screenshots. Review of knowledge was done well. One feature of this book you should take advantage of is the question section at the end of each chapter. I usually find these irritating and skip them because often the questions don’t reinforce learning and no answers are provided. The questions in this book *are* well constructed and the answers *are* provided.
Quibbles: Be prepared for a bit of keyboarding. O’Reilly provides a site for the book but the example code is not available for download. Your first bout of keying code may end in failure because the authors forget that in the first examples (the most important ones for an absolute beginner!) they need to recall being a beginner. They ask you to write out and save “a simple HTML document.” I did that. The example did not work on the server. I checked this, I checked that, I re-typed the code, I typed other examples. No luck. Then I stumbled onto changing the extension of my .html file to .php and all was well. See my point?
Friday, August 18, 2006
Creating myapp/index.php
<?php require_once("smarty.php");
$smarty->assign('test', '123');
$smarty->display('index.tpl');
?>
Chapter 10 Code • (110) Comments • (894) Trackbacks • Permalink
Monday, September 25, 2006
Cross-Site Scripting Attacks
To guard against these attacks, you should pass any strings that came from a user through the htmlentities function. It takes the format:
htmlentities(string_to_clean)
For example:
print "The title of the book is: " .
htmlentities($_POST['title']);
Here’s an example of what htmlentities does to the string:
<?php
$sample = "A sample is <i>italics</i>";
echo htmlentities($sample);
?>
When executed, this returns:
A sample is <i>italics</i>
Here’s a script to display the title table with the htmlentities functionality added:
<?php require_once('db_login.php'); require_once('DB.php');
$connection = DB::connect("mysql://$db_username:$db_password@$db_host/$db_database");
if (DB::isError($connection)){
die ("Could not connect to the database: <br />". DB::errorMessage($connection));
}
// Dislplay the table
$query = "SELECT * FROM `books`";
$result = $connection->query($query);
if (DB::isError($result)){
die("Could not query the database: <br />".$query." ".DB::errorMessage($result));
}
echo '<table border="1">';
echo "<tr><th>Title</th><th>Pages</th></tr>";
while ($result_row = $result->fetchRow(DB_FETCHMODE_ASSOC)) {
echo "<tr><td>";
echo htmlentities($result_row["title"]) . '</td><td>';
echo htmlentities($result_row["pages"]) . '</td></tr>';
}
echo "</table>";
$connection->disconnect();
?>
Chapter 12 Code • (5) Comments • (75) Trackbacks • Permalink
Wednesday, August 09, 2006
Example 03-01 All you need to start with PHP is a simple HTML document
<html>
<head>
<title>Hello World</title>
</head>
<body>
<p>I sure wish I had something to say.</p>
</body>
</html>
Thursday, July 06, 2006
Example 03-02 Adding PHP Code to an HTML File
<html>
<head>
<title>Hello World</title>
</head>
<body>
echo("<p>Now I have something to say.</p>");
</body>
</html>
This should be saved out with the .php extension, not the .html extension.
Chapter 3 Code • (137) Comments • (1862) Trackbacks • Permalink
Example 03-03 Calling echo() and print()
<html>
<head>
<title>Hello World</title>
</head>
<body>
<?php
echo ("Hello world!<br />");
print ('Goodbye.<br />');
print 'Over and out.';
?>
</body>
</html>
Chapter 3 Code • (4) Comments • (2310) Trackbacks • Permalink
Wednesday, August 09, 2006
Example 03-04 The HTML markup produced by the PHP code
<html>
<head>
<title>Hello World</title>
</head>
<body>
Hello world!<br />Goodbye.<br />Over and out.
</body>
</html>
Wednesday, July 19, 2006
Example 03-05 Using comments to make your code easier to read
<html>
<head>
<title>Hello World</title>
</head>
<body>
<?php
// A single line comment could say that we are going to
// print hello world.
/* This is how to do a
multi-line comment and could be used to comment out a block of code */
echo("Hello world!<br />");
print('Goodbye.<br />');
?>
</body>
</html>
Chapter 3 Code • (1) Comments • (368) Trackbacks • Permalink
Wednesday, August 09, 2006
Example 03-06 Reassigning a variable
<?php
$age = 30;
$age = 31;
echo $age;
?>
Wednesday, July 19, 2006
Example 03-07 The default handling of variable scope
<?php
// define a function function birthday(){
// Set age to 1
$age = 1;
}
// Set age to 30
$age = 30;
// Call the function
birthday();
// Display the age echo $age;
?>
Displays:
30
Chapter 3 Code • (5) Comments • (788) Trackbacks • Permalink
Example 03-08 Using a global variable changes the result
<?php
// Define a function function birthday(){
// Define age as a global variable
global $age;
// Add one to the age value
// set age to 30
$age = 30;
// Call the function birthday();
// Display the age echo $age;
?>
Displays:
31
Chapter 3 Code • (3) Comments • (97) Trackbacks • Permalink
Example 03-09 A static variable remembering its last value
<?php
// Define the function function birthday(){
// Define age as a static variable static $age = 0;
// Add one to the age value
$age = $age + 1;
// Print the static age variable
echo "Birthday number $age<br />";
}
// Set age to 30
$age = 30;
// Call the function twice birthday();
birthday();
// Display the age
echo "Age: $age<br />";
?>
Displays:
Birthday number 1
Birthday number 2
Age: 30
Chapter 3 Code • (4) Comments • (654) Trackbacks • Permalink
Example 03-10 PHP_SELF being used with a file called test.php
<?php
echo $_SERVER["PHP_SELF"];
?>
Outputs:
/test.php
Chapter 3 Code • (4) Comments • (503) Trackbacks • Permalink
Example 03-11 Working with strings
<?php
$my_string = "Margaritaville - Suntan Oil Application!";
echo "Margaritaville - Suntan Oil Application!";
?>
Chapter 3 Code • (4) Comments • (1002) Trackbacks • Permalink
Statistics
This page has been viewed 163780 times
Page rendered in 0.4565 seconds
Total Entries: 224
Total Comments: 16
Total Trackbacks: 307338
Most Recent Entry: 09/27/2006 12:39 pm
Most Recent Comment on: 10/26/2007 10:00 am
Total Members: 2
Total Logged in members: 0
Total guests: 14
Total anonymous users: 0
Most Recent Visitor on: 07/25/2008 03:55 am
The most visitors ever was 1103 on 11/20/2007 12:50 pm
