Top Banner
PHP (and a little MySQL) for the Default Library Techie PHP (and a Little MySQL) for the Default Library Techie Summer 2006 - This material has been created by Lisa Bartle for the Infopeople Project [infopeople.org], supported by the U.S. Institute of Museum and Library Services under the provisions of the Library Services and Technology Act, administered in California by the State Librarian. Any use of this material should credit the author and funding source. Lisa Bartle, Reference Librarian Cal. State University, San Bernardino [email protected] 909-537-7552 An Infopeople Workshop
119
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: ppt

PHP (and a little MySQL) for the Default Library Techie

PHP (and a Little MySQL) for the Default Library Techie Summer 2006 - This material has been created by Lisa Bartle for the Infopeople Project [infopeople.org], supported by the U.S. Institute of Museum and Library Services under the provisions of the Library Services and Technology Act, administered in California by the State Librarian. Any use of this material should credit the author and funding source.

Lisa Bartle, Reference Librarian

Cal. State University, San Bernardino

[email protected]

909-537-7552An Infopeople Workshop

Page 2: ppt

Introductions

•Lab assistant•Participants

–Name–Position–What you hope to do with PHP

Page 3: ppt

Assumptions for This Class

1. You know html code (e.g., tables, forms).

2. You understand the basics of databases (e.g., fields, records, truncation).

3. A little programming experience in any language is useful for this class but not essential (for example, you know what a variable is).

PHP (and a Little MySQL) for the Default Library Techie

Page 4: ppt

Goals for This Class

1. Not to teach everything about PHP, but provide the basic knowledge for an online database.

2. Explain just the code, not setting up a server.

3. Provide a solid grounding so the books are not as formidable.

PHP (and a Little MySQL) for the Default Library Techie

Page 5: ppt

Diagram of Web Applications

PHP (and a Little MySQL) for the Default Library Techie

Borrowed from MySQL/PHP Database Applications, 2nd ed. by Brad Bulger, Jay Greenspan, and David Wall.

Page 6: ppt

What is PHP?

A scripting language most suitable for online database management.

PHP (and a Little MySQL) for the Default Library Techie

Page 7: ppt

PHP Benefits

• Open source, so it’s free

• Hosting is inexpensive

• PHP can be embedded in html files using .php file extension

• Easy to learn

• Works powerfully and intimately with MySQL and the web

PHP (and a Little MySQL) for the Default Library Techie

Page 8: ppt

•Works well with Apache – also open source

•Parse complex strings

•Communicate with many credit card processing systems

•Communicate with many protocols, including IMAP & POP3 (email), NNTP (usenet), and DNS (hostnames & addresses)

•Native database support for over 25 db products including dBase, FilePro, FrontBase, IBM DB2, Hyperwave, mSQL, MySQL, Sybase, Unix dbm, Access, and many others

oAccess may be more difficult to connect

PHP (and a Little MySQL) for the Default Library Techie

PHP More Benefits

Page 9: ppt

Who’s Using PHP?

PHP (and a Little MySQL) for the Default Library Techie

Go to http://www.php.net/usage.php to find current usage statistics.

http://www.abc-lit.com

Page 10: ppt

Recommendationsfor Text Editors

• EditPlus2 from ES-Computing (free)

• NoteTab Light from Fookes Software (free)

• PHP Designer from MP Software (free)

• Crimson Editor from Ingyu Kang (free)

PHP (and a Little MySQL) for the Default Library Techie

Page 11: ppt

It All Starts with the Database

The MySQL Connection

PHP (and a Little MySQL) for the Default Library Techie

Page 12: ppt

What is MySQL?

• A database system often used for web-accessible databases

• A relational database management system (RDMS), like Microsoft’s Access, using SQL

PHP (and a Little MySQL) for the Default Library Techie

Page 13: ppt

MySQL Introduction

• Pronounced “My Ess Que Ell,” according to the MySQL reference manual.

• Can handle small databases or very large ones (ex. 50 million records, 60,000 tables and about 5,000,000,000 rows; maximum table size is 65536 terabytes, but operating systems have limitations).

Look at the MySQL reference manual (downloads.mysql.com/docs/refman-5.0-en.pdf).

PHP (and a Little MySQL) for the Default Library Techie

Page 14: ppt

MySQL Benefits

• Open source, so it’s free

• Designed to work intimately with PHP and the web

• Dependable code back to 1980s and fully backward compatible

• Can operate with a variety of operating systems:

o Unix; FreeBSD; Mac OS X; Solaris; Windows 9x, Me, NT, 2000, XP, and Server 2003(Microsoft products are not the best with MySQL)

PHP (and a Little MySQL) for the Default Library Techie

Page 15: ppt

How Is Your Data Stored?

• Access

• Excel

• Something else

PHP (and a Little MySQL) for the Default Library Techie

Page 16: ppt

If You Have Data …

• Using Access or Excel you may want to convert to MySQL using an online converter

• Intelligent Converters has several for-fee programs at http://www.convert-in.com

PHP (and a Little MySQL) for the Default Library Techie

Page 17: ppt

If You Don’t Have Data …• Create a database using MySQL client

PHP (and a Little MySQL) for the Default Library Techie

Page 18: ppt

More If You Don’t Have Data …

• Create a database using Access or Excel

• Create a database using phpMyAdmino a tool written in PHP for the administration of a

MySQL server over the Web o often included with the operating system

PHP (and a Little MySQL) for the Default Library Techie

Page 19: ppt

phpMyAdmin Table Creation

PHP (and a Little MySQL) for the Default Library Techie

Page 20: ppt

To Maintain Your Data• Use Access or Excel to update, then convert

to MySQL using a converter

PHP (and a Little MySQL) for the Default Library Techie

Page 21: ppt

INSERT INTO 'people' VALUES('Joann', 'M', 'Aardappel', "", 'Pine Bluff', 'AR', 'Fontana', 'CA', 'San Bernardino County Sun', '2000-10-09');

INSERT INTO 'people' VALUES('Louie', 'R', 'Aboytes', "", 'Mesa', 'AZ', 'Loma Linda', 'CA', 'San Bernardino County Sun', '2001-01-05');

INSERT INTO 'people' VALUES('Edith', "", 'Abramson', 'Gimble', 'St. Paul', 'MN', 'Redlands', 'CA', 'San Bernardino County Sun', '2000-11-18');

INSERT INTO 'people' VALUES('Antonia', "", 'Acevedo', 'Bermudez', "", 'Mexico', 'Fontana', 'CA', 'San Bernardino County Sun', '2000-11-14');

MySQL data

PHP (and a Little MySQL) for the Default Library Techie

Page 22: ppt

To Maintain Your Data

• Use Access or Excel to update, then convert to MySQL using a converter

• Use phpMyAdmin to update

PHP (and a Little MySQL) for the Default Library Techie

Page 23: ppt

phpMyAdmin Data Input

PHP (and a Little MySQL) for the Default Library Techie

Page 24: ppt

Housekeeping

1. Open Internet Explorer

2. Go to Tools > Internet Options > Advanced

3. Put an X next to: Enable folder view for FTP sites

PHP (and a Little MySQL) for the Default Library Techie

Page 25: ppt

Our Project

PHP (and a Little MySQL) for the Default Library Techie

Go to http://phpclass.infopeople.org/search.html

Page 26: ppt

The PHP Connection

PHP (and a Little MySQL) for the Default Library Techie

Page 27: ppt

•PHP code starts <?php

•PPH code ends ?>

PHP Start and Stop

example1.php

PHP (and a Little MySQL) for the Default Library Techie

Page 28: ppt

Comments

PHP (and a Little MySQL) for the Default Library Techie

• Explain the purpose of the coding

• Help you and others read the code

• Help you when you’ve forgotten what it all means

Page 29: ppt

Comment Structure

PHP (and a Little MySQL) for the Default Library Techie

// begin each line inside the PHP code

# begin each line inside the PHP code

/* begin and */ end several lines inside the PHP code

Page 30: ppt

Comment Examples

PHP (and a Little MySQL) for the Default Library Techie

<?php//Purpose: to create a form for patrons’ questions?>----------------------------<?php###### Show Results to User ######?>----------------------------<?php/*Purpose: to create a form for patrons’ questionsAuthor: Lisa BartleDate: Nov. 6, 2006*/?>

Page 31: ppt

Exercise 1

PHP (and a Little MySQL) for the Default Library Techie

Open up Notepad and get to coding!

Page 32: ppt

“Hello_Librarians”:First PHP Example

example1.php

PHP (and a Little MySQL) for the Default Library Techie

<h2>Infopeople PHP class</h2><?phpprint "<p>Hello librarians!";?>

Code

User Sees

Page 33: ppt

PHP and HTML

•HTML can be outside of PHP coding

•HTML can be inside of the code, but must be inside quotation marks with a print statement

•PHP statements end in a semi-colon

example1.php

PHP (and a Little MySQL) for the Default Library Techie

Page 34: ppt

Outputting Data

PHP (and a Little MySQL) for the Default Library Techie

We will use PRINT in this class.

•Echo -- a language construct or structure for a single argument.

•Print -- a language construct, which can have multiple arguments and more complex expressions.

Page 35: ppt

Echo & Print Example

example2.php

PHP (and a Little MySQL) for the Default Library Techie

<?phpEcho "Infopeople PHP Presentation"; Print "<P>Hello librarians.";?>

Code

User Sees

Page 36: ppt

What is a Variable?

PHP (and a Little MySQL) for the Default Library Techie

• A variable is a place to store information.

• Information stored in a variable can be defined by the programmer.

• Information stored in a variable can come from a user using a form.

Page 37: ppt

Variables

PHP (and a Little MySQL) for the Default Library Techie

• A variable always begins with a $ then the variable name.

• Variables are case sensitive and alpha-numeric.

• Do not start a variable with a number.

• All the variables below are different.$book$amount$Amount$AmoUnT$AMOUNT

Page 38: ppt

Variable Example

example3.php

PHP (and a Little MySQL) for the Default Library Techie

<?php Print "<h2>Infopeople PHP Presentation </h2><br>";print "<p>Hello librarians! <br>"; $date="Nov. 6, 2006";print "Today’s date is $date";?>

Page 39: ppt

Printing Variables

example3.php

PHP (and a Little MySQL) for the Default Library Techie

<?php $date="Nov. 6, 2006";print "Today’s date is $date";Print "Today’s date is "; print $date;

?>

Page 40: ppt

Date Function

PHP (and a Little MySQL) for the Default Library Techie

example7.php

• Use date() to display the date and time

• Can display in multiple ways

Jul. 12, 2006

July 12, 2006

7/12/2006

2006/07/12

Page 41: ppt

Date Display$datedisplay=date("Y/m/d");print $datedisplay;# If the date is November 6, 2005# It would display as 2005/11/06

$showdate=date("l, F j, Y");print $showdate;# If the date is November 6, 2005# It would display as# Sunday, November 6, 2005

PHP (and a Little MySQL) for the Default Library Techie

example7.php

See handout “Displaying the Date Using Date()”

Page 42: ppt

Exercise 2

PHP (and a Little MySQL) for the Default Library Techie

Open up Notepad and get to coding!

Page 43: ppt

PHP (and a Little MySQL) for the Default Library Techie

Profession Example

profession.php

Page 44: ppt

The HTML Connection

PHP (and a Little MySQL) for the Default Library Techie

Page 45: ppt

Forms

PHP (and a Little MySQL) for the Default Library Techie

Forms provide an interface for users to query your database.

Page 46: ppt

Forms HTML

PHP (and a Little MySQL) for the Default Library Techie

<-- form tag goes before the form objects --><form>

[form objects go here][radio buttons, pull-down menus, text boxes]

[submit button, cancel button]

</form>

Page 47: ppt

Radio Buttons

PHP (and a Little MySQL) for the Default Library Techie

example9.html

Name becomes the variable name. Value is what the variable is holding.

<INPUT type="radio" name="sex" value="M"> Male<BR> <INPUT type="radio" name="sex" value="F"> Female<BR>

Page 48: ppt

Pull-Down MenusPHP (and a Little MySQL) for the Default Library Techie

example10.html

<p>City of death: <select name="deathcity" size="1"><option selected value="">Search all cities in database</option><option value="Fontana">Fontana</option><option value="Loma Linda">Loma Linda</option><option value="Redlands">Redlands</option><option value="San Bernardino">San Bernardino</option><option value="Victorville">Victorville</option></select>

Name =variable name. Value =what variable is holding.

Page 49: ppt

Text Boxes

PHP (and a Little MySQL) for the Default Library Techie

example11.html

Name =variable name.

<input TYPE= "text" NAME="anyname" SIZE="20" MAXLENGTH="40">

Page 50: ppt

Check Boxes

PHP (and a Little MySQL) for the Default Library Techie

example13.html

Libraries should get more<p><input TYPE="checkbox" NAME="money" VALUE="Yes" checked> Money <br><input TYPE="checkbox" NAME="respect" VALUE="Yes"> Respect

Name=variable nameValue=what variable is holding.

Page 51: ppt

Submit/Reset Buttons

PHP (and a Little MySQL) for the Default Library Techie

example14.html

<input TYPE="submit" NAME="Search" VALUE="Search it!"> <input TYPE="reset" NAME="Cancel" VALUE="Clear Form">

Page 52: ppt

Form Action

PHP (and a Little MySQL) for the Default Library Techie

<-- not just form tag, but form action -->

<form action="file,URL,or path" method="post">

[form objects here]

</form>

Page 53: ppt

The PHP Connection

PHP (and a Little MySQL) for the Default Library Techie

Page 54: ppt

Operators

• Arithmetic operators

• Assignment operators

• Comparison operators

PHP (and a Little MySQL) for the Default Library Techie

See handout “Operators.”

Page 55: ppt

Arithmetic Operators

$a + $b //addition$a - $b //subtraction$a * $b //multiplication$a / $b //division

example6.php

PHP (and a Little MySQL) for the Default Library Techie

Page 56: ppt

Assignment & Arithmetic Operators

<?php$a=15;$b=30;$total=$a+$b;Print $total;Print "<p><h1> $total </h1>";//total is 45 ?>

example6.php

PHP (and a Little MySQL) for the Default Library Techie

Page 57: ppt

Comparison OperatorsEqual $a == $b

Not Equal $a != $b$a <> $b

Less Than $a < $b

Less Than or Equal to $a <= $b

Greater Than $a > $b

Greater Than or Equal to $a >= $b

PHP (and a Little MySQL) for the Default Library Techie

See handout “Operators”

Page 58: ppt

Difference Between = and ==

= Assigns a value to a variable

== Checks the value of a variable

PHP (and a Little MySQL) for the Default Library Techie

See handout “Operators.”

Page 59: ppt

If … Else

If ($user=="Lisa") { Print "Hello Lisa."; }Else {Print "You aren't Lisa.";}

No THEN in PHP

PHP (and a Little MySQL) for the Default Library Techie

If (condition){

Statement;}Else {

Statement;}

Page 60: ppt

ElseIf

If ($user=="Lisa") {Print "Hello Lisa."; }Elseif ($user=="Mo") {Print "Hello Mo."; }Else {Print "Who are you?"; }

PHP (and a Little MySQL) for the Default Library Techie

If (condition){

Statement;}Elseif (condition){

Statement;}Else{

Statement;}

Page 61: ppt

While Loops <?php$count=0;While ($count<5){Print "Hello Lisa.";$count=$count + 1;}#Another way to say #to say that is #$count++;?>

example8.php

PHP (and a Little MySQL) for the Default Library Techie

While (condition){ Statement;}

Page 62: ppt

PHP (and a Little MySQL) for the Default Library Techie

Profession Example

profession.php

1. Go to http://phpclass.infopeople.org/lisa/profession.php

Page 63: ppt

PHP (and a Little MySQL) for the Default Library Techie

Questions to Think About

example15.php

1. What is the first step for this code?

2. What kind of php structures will be necessary?

Page 64: ppt

Exercise 3

PHP (and a Little MySQL) for the Default Library Techie

Page 65: ppt

Top Errors & Messages

PHP (and a Little MySQL) for the Default Library Techie

See handout

“Common PHP Mistakes and Error Messages”

1. Missing semi-colon.

2. Misspelled variables and missing $.

3. Incomplete pairs for quotation marks, parentheses, curly brackets, etc.

Page 66: ppt

Our Sample Database

PHP (and a Little MySQL) for the Default Library Techie

• This class’s prepared database is called obituaries.

• The file is obituaries.sql.

• It is an index of people’s names who have had obits listed in the newspaper. It is for patron’s genealogical research.

• The table called people has our data.Records: 100Fields: 11

See handout “Organize Your Database and Server

Information”

Page 67: ppt

Download Search.html to Your Directory

PHP (and a Little MySQL) for the Default Library Techie

1. Click View > Source2. Save the file as search.html to your desktop.

Go to http://phpclass.infopeople.org/search.html

3. Copy the file from your desktop into your directory.

You will not need to create the search form from scratch.

Page 68: ppt

search.php

PHP (and a Little MySQL) for the Default Library Techie

>>Creates connection to server & database

•Alters input for safety

•Creates SQL query

•Displays the results

>>Close the connection to database

Page 69: ppt

Connect to the Server

PHP (and a Little MySQL) for the Default Library Techie

mysql_connect("hostaddress", "myusername", "mypassword") or die ("message");

Die() is a function for an output message and to terminate the current script.

Mysql_connect() is the function used to connect to the server.

Page 70: ppt

Mysql_connect() Structure

PHP (and a Little MySQL) for the Default Library Techie

$connection=mysql_connect("hostaddress", "myusername", "mypassword") or die ("message");

Page 71: ppt

Mysql_connect() Example

PHP (and a Little MySQL) for the Default Library Techie

# Create connection to database# Define variables$hostaddress="phpclass.phpclass.infopeople.org";$myusername="phpclass";$mypassword="infopeople";

$connection=mysql_connect($hostaddress, $myusername, $mypassword) or die ("Can’t connect to server.");

Page 72: ppt

Close Connection

PHP (and a Little MySQL) for the Default Library Techie

Mysql_close() is the function to close the connection to the server.

mysql_close($connection);

Page 73: ppt

Connect to the Database

PHP (and a Little MySQL) for the Default Library Techie

Mysql_select_db() is the function to select your MySQL database.

mysql_select_db("database") or die ("Message");

Page 74: ppt

Mysql_select_db()

PHP (and a Little MySQL) for the Default Library Techie

#$database="obituaries";

#$tablename="people";

$result=mysql_select_db($database) or die("Database could not be selected");

Page 75: ppt

Alter Input for Safety

PHP (and a Little MySQL) for the Default Library Techie

• Creates connection to database

>>Alters input for safety

• Creates SQL query

• Displays the results

• Close the connection to database

Page 76: ppt

Trim(), Strip_tags(), Strtolower()

PHP (and a Little MySQL) for the Default Library Techie

search.php

$anyname=trim($anyname);

$anyname=strip_tags($anyname);

$anyname=strtolower($anyname);

• Trim removes whitespace at the beginning and end of a string.

• Strip_tags tries to remove PHP & HTML tags from a string.

• Strtolower converts alpha characters to lowercase.

Page 77: ppt

Exercise 4

PHP (and a Little MySQL) for the Default Library Techie

Open up Notepad and get to coding!

Page 78: ppt

Create SQL Query

PHP (and a Little MySQL) for the Default Library Techie

• Creates connection to database

• Alters input for safety

>>Creates SQL query

• Displays the results

• Close the connection to database

Page 79: ppt

Concatenation

<?php

$string1="Hello";

$string2="Librarians";

$string3=$string1." ".$string2;

Print $string3;

print "<p>";

Print $string1." ".$string2;

?>

example5.php

PHP (and a Little MySQL) for the Default Library Techie

“A union by chaining or linking together.” Use a period to link together strings.

Page 80: ppt

Escaping the Character

<?php$heading="\"Lisa's Books\"";Print $heading;?>

PHP (and a Little MySQL) for the Default Library Techie

If the string has a set of double quotation marks that must remain visible, use the \ [backslash] before the quotation marks so PHP does not act on them and only displays them.

example16.php

Page 81: ppt

The SQL Connection

PHP (and a Little MySQL) for the Default Library Techie

Page 82: ppt

Diagram of Web Applications

PHP (and a Little MySQL) for the Default Library Techie

Borrowed from MySQL/PHP Database Applications, 2nd ed. by Brad Bulger, Jay Greenspan, and David Wall.

Page 83: ppt

What is SQL?

• Structured Query Language

• A database access language standard

• All SQL-capable databases support a common subset of SQL

o Access, MySQL, some Oracle databases, etc.

PHP (and a Little MySQL) for the Default Library Techie

Page 84: ppt

SQL Select Query Template

SELECT [field],[field] from [table] WHERE [field] [operator] [parameter or input] ORDER BY [field] [ASC or DESC];

PHP (and a Little MySQL) for the Default Library Techie

Page 85: ppt

SQL Query Examples

•SELECT lastname, firstname from people where newsyear > 2000;

•SELECT lastname, firstname from people where newsyear > 2000 and newsyear < 2003;

•SELECT lastname, firstname from people where newsyear BETWEEN 2000 and 2003;

PHP (and a Little MySQL) for the Default Library Techie

Database: obituaries

Table: people

Fields: lastname, firstname, middlename,

maidenname, newsdate, newsyear

Numbers do not need quotation marks in SQL.

Page 86: ppt

•SELECT * from people where lastname = "Jones";

•SELECT * from people where lastname LIKE "Jones%";

•SELECT * from people where lastname LIKE "%Jones";

•SELECT * from people where lastname LIKE "%Jones%" ORDER BY lastname ASC, firstname ASC;

PHP (and a Little MySQL) for the Default Library Techie

SQL Query ExamplesDatabase: obituaries

Table: people

Fields: lastname, firstname, middlename,

maidenname, newsdate, newsyear

Text needs quotation marks in SQL.

Page 87: ppt

PHP (and a Little MySQL) for the Default Library Techie

PHP & SQL in the Query

SELECT * from people where lastname LIKE "%Jones%" ORDER BY lastname ASC;

$query="SELECT * from people where lastname LIKE \"%Jones%\" ORDER BY lastname ASC";

Page 88: ppt

PHP (and a Little MySQL) for the Default Library Techie

Variables in the PHP/SQL Query

$query="SELECT * from people where lastname LIKE \"%Jones%\" ORDER BY lastname ASC";

$query="SELECT * from " .$tablename. " where lastname LIKE \"%" .$anyname. "%\" ORDER BY lastname ASC";

Page 89: ppt

Match Against In Boolean Mode

PHP (and a Little MySQL) for the Default Library Techie

$query="SELECT * from " .$tablename. " WHERE MATCH(firstname, middlename, lastname, maidenname) AGAINST ('$anyname' IN BOOLEAN MODE) AND newsyear>= " .$newsyear1. " AND newsyear<=" .$newsyear2. " ORDER BY lastname ASC";

Red=PHP

Purple=SQL

Page 90: ppt

SQL Troubleshooting Tips

PHP (and a Little MySQL) for the Default Library Techie

Build the SQL statement slowly, piece by piece.

# Keep what is working and comment it out so # that you can start from success when you # need to start over.# $query="SELECT * from ".$tablename; # This works$query="SELECT * from ".$tablename;Print the query so you can tell where it is breaking down.Print $query;

Page 91: ppt

If ($deathcity=="") {$deathcityquery=" ";}

else {$deathcityquery=" and deathcity=\"".$deathcity."\"";}

PHP (and a Little MySQL) for the Default Library Techie

SQL Query

Page 92: ppt

If ($anyname=="") {$query="SELECT * from " . $tablename. " WHERE newsyear >=".$newsyear1." AND newsyear<=".$newsyear2.$deathcityquery." ORDER BY lastname ASC";

}else{$query="SELECT * from " . $tablename. " WHERE match(firstname, middlename, lastname, maidenname) against ('$anyname' in boolean mode) and newsyear >=".$newsyear1." AND newsyear<=".$newsyear2.$deathcityquery." ORDER BY lastname ASC";

• }

PHP (and a Little MySQL) for the Default Library Techie

More SQL Query

Page 93: ppt

Download sql_query.php

PHP (and a Little MySQL) for the Default Library Techie

Sql_query.php is available in the main directory of the ftp server.

• Copy the file to your desktop. • Open it. • Copy the query and place it under the header Create SQL Query.

Page 94: ppt

True Boolean Queries

(peek or byrd) and (mary or ellen)

PHP (and a Little MySQL) for the Default Library Techie

How do we do it?

We have a textbox, $anyname, in our obituary search form. We want it to be Boolean searchable. Such as:

Page 95: ppt

Alter the Input for SQL

PHP (and a Little MySQL) for the Default Library Techie

Think of our old AltaVista searches:

+alice –wonderland +columbia

We need to use PHP to substitute the AND, OR, NOT that the users input so that SQL can understand it.

Page 96: ppt

Create Boolean String

PHP (and a Little MySQL) for the Default Library Techie

# Insert an initial plus-sign so # the first term or phrase is # included as a mandatory.$addplus="+";$anyname=$addplus.$anyname;

Page 97: ppt

PHP (and a Little MySQL) for the Default Library Techie

Replacing Strings

Use str_replace() to replace one string with another.

$string= str_replace("find this", "replace with this", $in_ this_string);

Page 98: ppt

# Replace all the AND's with +'s, # so that SQL’s fulltext indexing# can understand it.# The spacing is important here.$anyname=str_replace(" and ", " +", $anyname);

PHP (and a Little MySQL) for the Default Library Techie

Create Boolean Stringstr_replace() AND

Page 99: ppt

PHP (and a Little MySQL) for the Default Library Techie

Create Boolean Stringstr_replace() OR

# OR is assumed, so just pull# those out.$anyname =str_replace(" or ", " ", $anyname);

Page 100: ppt

PHP (and a Little MySQL) for the Default Library Techie

Create Boolean Stringstr_replace() NOT

# look for NOTs$anyname=str_replace(" not ", " -", $anyname);# Some people may put AND NOT. Since# we already replaced the ANDs and # ORs, this may look strange.

$anyname=str_replace(" +not ", " -", $anyname);

Page 101: ppt

Mysql_query()

PHP (and a Little MySQL) for the Default Library Techie

$result=mysql_query($query) or die("Couldn’t execute query.");

mysql_query($query) or die(“Message.");

Where $query is the constructed SQL query:

Page 102: ppt

Exercise 5

PHP (and a Little MySQL) for the Default Library Techie

Open up Notepad and get to coding!

Page 103: ppt

search.php

PHP (and a Little MySQL) for the Default Library Techie

• Creates connection to database

• Alters input for safety

• Creates SQL query

>>Displays the results

• Close the connection to database

Page 104: ppt

What is an Array?

PHP (and a Little MySQL) for the Default Library Techie

An array is a variable that holds many different variables in it.

“An array of books.”

Page 105: ppt

$result is an Array

where the results are stored.

PHP (and a Little MySQL) for the Default Library Techie

$result=mysql_query($query) or die("Couldn’t execute query.");

Page 106: ppt

$counter=0;while ($rows=mysql_fetch_array($result)) # displaying data from results{

Display row after row here with html.}

Mysql_fetch_array()

PHP (and a Little MySQL) for the Default Library Techie

Use mysql_fetch_array() to gather the waiting query results row by row, then display them.

Page 107: ppt

PHP (and a Little MySQL) for the Default Library Techie

Mysql_num_rows()

Use mysql_num_rows to get the number of matching results. $Numrows is the number of rows, or records, in the result of the query.

$numrows=mysql_num_rows($result);

Page 108: ppt

There are X result(s) in your search.

1. lastname (maidenname), firstname middlename Born: birthcity, birthstate

Died: deathcity, deathstate Obituary: <i>newspaper</i> newsdate, newsyear

Plan the Display

PHP (and a Little MySQL) for the Default Library Techie

Write out how you want it to look!

Page 109: ppt

$counter=0;$numrows=mysql_num_rows($result);Print "There are $numrows result(s) in your search.<p>"

Display Results to User

PHP (and a Little MySQL) for the Default Library Techie

Page 110: ppt

while ($rows=mysql_fetch_array($result)) # filling-in data from results{$counter=$counter+1;Print $counter.". ".$rows["lastname"]. " (" . $rows["maidenname"] ."), " . $rows["firstname"] ." " . $rows["middlename"]."<br>";}

Display Results to User

PHP (and a Little MySQL) for the Default Library Techie

Red is HTML and the quotation marks surrounding it.

Page 111: ppt

Review of PHP

PHP (and a Little MySQL) for the Default Library Techie

Page 112: ppt

search.php

PHP (and a Little MySQL) for the Default Library Techie

• Creates connection to database

• Alters input for safety

• Creates SQL query

• Displays the results

>>Close the connection to database

Page 113: ppt

mysql_close($connection);

Close Connection

PHP (and a Little MySQL) for the Default Library Techie

Mysql_close is the function to close the connection to the server.

Page 114: ppt

Exercise 6

PHP (and a Little MySQL) for the Default Library Techie

Open up Notepad and get to coding!

Page 115: ppt

Include Files & Require Files

Include and require insert files (sensitive or oft-repeated code stored in files) into the current code. This will prove useful and protective once you connect to a database, as well as for other repeated functions.

PHP (and a Little MySQL) for the Default Library Techie

example17.inc

Include "/path/to/filename.html";

include ("footer.txt");

require ("start.inc");

Page 116: ppt

Include Files & Require Files

PHP (and a Little MySQL) for the Default Library Techie

<?php

$hostaddress="phpclass.phpclass.infopeople.org";$username="phpclass";$password="infopeople";$database="obituaries";$tablename="people";

?>

example17.inc

The file start.inc might look like:

Page 117: ppt

Include File Placement

PHP (and a Little MySQL) for the Default Library Techie

### Define Connection Variables ###

Require ("start.inc");

### Connect to Serve & Database ###

$connection=mysql_connect($hostaddress, $myusername, $mypassword) or die ("Can’t connect to server.");

Page 118: ppt

PHP Covered

PHP (and a Little MySQL) for the Default Library Techie

date()die()echoelseifif … elseinclude()mysql_close()mysql_connect()mysql_fetch_array()mysql_num_rows()mysql_query()mysql_select_db()

printrequire()str_replace()strip_tagsstrtolower()trim()while#///* & */.\

19 down,

4592 to go!

Page 119: ppt

Evaluation Time

Please go to

infopeople.org/wseval.html

and complete the evaluation.

PHP (and a Little MySQL) for the Default Library Techie

Thank you!