Chapter 3: Getting to Know PHP and MySQL

In This Chapter

arrow.png Working with PHP and MySQL

arrow.png Creating a page with PHP

arrow.png Managing a database with MySQL

In Book VI, you dig into the code necessary to create functions and features on your website. Many, if not all, of these functions and features use PHP Hypertext Preprocessor (PHP) tags. When combined with the WordPress code, these tags make things happen (such as displaying post content, categories, archives, links, and more) on your website.

One of the reasons WordPress is the most popular content management system (CMS) is that you don’t need to know PHP code to use it. That’s to say, you can use WordPress easily without ever looking at any of the code or template files contained within it. However, if you want to tweak the settings of your WordPress theme (flip to Book VI) or the code of a particular plugin (see Book VII), you need to understand some basics of how PHP works. But don’t worry; you don’t need to be a PHP programmer.

This chapter introduces you to the very basics of PHP and MySQL, which is the database system that stores your WordPress data. After you read this chapter, you’ll understand how PHP and MySQL work together with the WordPress platform to serve up your website in visitors’ browsers.

remember.eps This book doesn’t turn you into a PHP programmer or MySQL database administrator, but it gives you a glimpse of how PHP and MySQL work together to help WordPress build your website. If you’re interested in finding out how to program PHP or become a MySQL database administrator, check out PHP & MySQL For Dummies by Janet Valade.

Understanding How PHP and MySQL Work Together

WordPress uses a PHP/MySQL platform, which provides everything you need to create your own blog and publish your own content dynamically, without knowing how to program those pages. In short, all your content is stored in a MySQL database in your hosting account.

technicalstuff.eps PHP is a server-side scripting language for creating dynamic web pages. When a visitor opens a page built in PHP, the server processes the PHP commands and then sends the results to the visitor’s browser. MySQL is an open source relational database management system (RDBMS) that uses Structured Query Language (SQL), the most popular language for adding, accessing, and processing data in a database. If that all sounds like Greek to you, just think of MySQL as a big file cabinet where all the content on your blog is stored.

Every time a visitor goes to your blog to read your content, he makes a request that’s sent to a host server. The PHP programming language receives that request, makes a call to the MySQL database, obtains the requested information from the database, and then presents the requested information to your visitor through his web browser.

Here content refers to the data stored in the MySQL database; that is, your blog posts, pages, comments, links, and options that you set up on the WordPress Dashboard. However, the theme (or design) you choose to use for your blog — whether it’s the default theme, one you create, or one you have custom designed — isn’t part of the content in this case. Theme files are part of the file system and aren’t stored in the database. Therefore, it’s a good idea to create and keep a backup of any theme files that you’re currently using. See Book VI for further information on WordPress theme management.

remember.eps Make sure your web host backs up your site daily so that your content (data) won’t be lost in case something happens. Web hosting providers who offer daily backups as part of their services can save the day by restoring your site to its original form. Additionally, Book II, Chapter 5 covers important information about backing up your website.

Exploring PHP Basics

WordPress requires PHP in order to work; therefore, your web hosting provider must have PHP enabled on your web server. If you already have WordPress up and running on your website, you know PHP is running and working just fine. Currently, the PHP version required for WordPress is version 5.2.4 or later.

Before you play around with template tags (covered in Book VI) in your WordPress templates or plugin functions, you need to understand what makes up a template tag and why, as well as the correct syntax, or function, for a template tag as it relates to PHP. Additionally, have a look at the WordPress files contained within the download files. Many of the files end with the .php file extension — an extension required for PHP files, which separates them from other file types, such as JavaScript (.js) or CSS (.css).

As I state earlier, WordPress is based in PHP (a scripting language for creating web pages) and uses PHP commands to pull information from the MySQL database. Every tag begins with the function to start PHP and ends with a function to stop it. In the middle of those two commands lives the request to the database that tells WordPress to grab the data and display it.

A typical template tag, or function, looks like this:

<?php get_info(); ?>

This example tells WordPress to do three things:

check Start PHP: <?php

check Use PHP to get information from the MySQL database and deliver it to your blog: get_info();

check Stop PHP: ?>

In this case, get_info() represents the tag function, which grabs information from the database to deliver it to your blog. The information retrieved depends on what tag function appears between the two PHP commands.

remember.eps Every PHP command you start requires a stop command. For every <?php, you must include the closing ?> command somewhere later in the code. PHP commands structured improperly cause ugly errors on your site, and they've been known to send programmers, developers, and hosting providers into loud screaming fits. You find a lot of starting and stopping of PHP throughout the WordPress templates and functions. The process seems as though it would be resource intensive, if not exhaustive, but it really isn't.

warning_bomb.eps Always, always make sure that the PHP start and stop commands are separated from the function with a single space. You must have a space after <?php and a space before ?> — if not, the PHP function code doesn't work. So make sure that the code looks like this: <?php get_info(); ?> — not like this: <?phpget_info();?>

Trying Out a Little PHP

To test some PHP code, follow these steps to create a simple HTML web page with an embedded PHP function:

1. Open a new, blank file in your default text editor — Notepad (Windows) or TextMate (Mac) — type <html>, and then press Enter.

The <html> tag tells the web browser that this is an HTML document and should be read as a web page.

2. Type <head> and then press Enter.

The <head> HTML tag contains elements that tell the web browser about the document; this information is read by the browser but hidden from the web page visitor.

3. Type <title>This is a Simple PHP Page</title> and then press Enter.

The <title> HTML tag tells the browser to display the text between two tags as the title of the document in the browser title bar. (Note: All HTML tags need to be opened and then closed, just like PHP tags that I describe in the preceding section. In this case the <title> tag opens the command, and the </title> tag closes it and tells the web browser that you're finished dealing with the title.)

4. Type </head> to close the <head> tag from Step 2 and then press Enter.

5. Type <body> to define the body of the web page and then press Enter.

Anything that appears after this tag displays in the web browser window.

6. Type <?php to tell the web browser to start a PHP function and then press the spacebar.

See the preceding section on starting and stopping PHP functions.

7. Type echo ‘<p>Testing my new PHP function</p>’; and then press the spacebar.

This is the function that you want PHP to execute on your web page. This particular function echoes the text “Testing my new PHP function” and displays it on your website.

8. Type ?> to tell the web browser to end the PHP function and then press Enter.

9. Type </body> to close the <body> HTML tag from Step 5 and then press Enter.

This tells the web browser that you’re done with the body of the web page.

10. Type </html> to close the <html> tag from Step 1 and then press Enter.

This tells the web browser that you’re at the end of the HTML document.

When you’re done with Steps 1–10, double-check that the code in your text editor looks like this:

<html>

<head>

<title>This is a Simple PHP Page</title>

   </head>

   <body>

   <?php echo '<p>Testing my new PHP function</p>'; ?>

   </body>

   </html>

After you write your code, follow these steps to save and upload your file:

1. Save the file to your local computer as testing.php.

2. Upload the testing.php file.

Via File Transfer Protocol, upload testing.php to the root directory of your web server. If you need a review on how to use FTP to transfer files to your web server, look through the information presented in Book II, Chapter 2.

3. Open a web browser and type the address (http://yourdomain.com/testing.php) in the web browser’s address bar (where yourdomain is your actual domain name).

A single line of text displays: Testing my new PHP function, as shown in Figure 3-1.

If the testing.php file displays correctly in your browser, congratulations! You programmed PHP to work in a web browser!

tip.eps If the testing.php file doesn't display correctly in your browser, a PHP error message gives you an indication of the errors in your code. (Usually included with the error message is the line number where the error exists in the file.)

9781118383339-fg020301.tif

Figure 3-1: A basic PHP page in a browser window.

Managing Your MySQL Database

Many new WordPress users are intimidated by the MySQL database, perhaps because it seems to be way above their technical skills or abilities. Truth be told, regular users of WordPress — those who just use it to publish content — don’t really ever have to dig into the database unless they want to. You need to explore the database only if you’re dealing with theme or plugin development, or if you’re contributing code to the WordPress project. This section gives you a basic overview of the WordPress database stored in MySQL so that you have an understanding of the structure and know where items are stored.

tip.eps Currently, WordPress requires MySQL version 5.0 (or greater) in order to work correctly. If your web hosting provider doesn’t have 5.0 (or greater) installed on your web server, kindly ask to upgrade.

After WordPress is installed on your server (which I discuss in Chapter 4 of this minibook), the database gets populated with 11 tables that exist to store different types of data from your WordPress blog. Figure 3-2 displays the structure of the tables, as follows:

9781118383339-fg020302.tif

Figure 3-2: The WordPress database structure.

check wp_commentmeta: This table stores every comment published to your site and contains information, or metadata, that includes

• A unique comment ID number

• A comment meta key, meta value, and meta ID (unique numerical identifiers assigned to each comment left by you, or visitors, on your site)

check wp_comments: This table stores the body of the comments published to your site, including

• A post ID that specifies which post the comment belongs to

• The comment content

• The comment author’s name, URL, IP address, and e-mail address

• The comment date (day, month, year, and time)

• The comment status (approved, unapproved, or spam)

check wp_links: This stores the name, URL, and description of all links you create by using the WordPress Link Manager. It also stores all the advanced options for the links you created, if any.

check wp_options: This stores all the option settings that you set for WordPress after you install it, including all theme and plugin option settings.

check wp_postmeta: This includes all posts or pages published to your site and contains metadata that includes

• The unique post ID number. (Each blog post has a unique ID number to set it apart from the others.)

• The post meta key, meta value (unique numerical identifiers for each post created on your site), and any custom fields you’ve created for the post.

check wp_posts: This table features the body of any post or page you've published to your blog, including autosaved revisions and post option settings, such as

• The post author, date, and time

• The post title, content, and excerpt

• The post status (published, draft, or private)

• The post comment status (open or closed)

• The post type (page, post, or custom post type)

• The post comment count

check wp_terms: This stores the categories you've created for posts and links as well as tags that have been created for your posts.

check wp_term_relationships: This stores the relationships among the posts as well as the categories and tags that have been assigned to them.

check wp_term_taxonomies: WordPress has three types of taxonomies by default: category, link, and tag. This table stores the taxonomy associated for the terms stored in the wp_terms table.

check wp_usermeta: This table features metadata from every user with an account on your WordPress website. This metadata includes

• A unique user ID

• A user meta key, meta value, and meta ID, which are unique identifiers for users on your site

check wp_users: The list of users with an account on your WordPress website is maintained within this table and includes

• The username, first name, last name, and nickname

• The user login

• The user password

• The user e-mail

• The registration date

• The user status and role (subscriber, contributor, author, editor, or administrator)

Most web hosting providers give you a utility, or an interface, to view your MySQL database, and the most common one is phpMyAdmin (as shown in Figure 3-2). If you’re unsure how you can view your database on your hosting account, get in touch with your hosting provider to find out.

When the multisite feature in WordPress is activated (check out Book VIII for information about the multisite feature), WordPress adds six additional tables in the database:

check wp_blogs: This table stores information about each blog created in your network, including

• A unique blog numerical ID

• A unique site ID number (determines the ID of the site the blog belongs to)

• The blog domain

• The blog server path

• The date the blog was registered

• The date the blog was updated

• The blog status (public, private, archived, spam; see Book VIII for more information on blog status)

check wp_blog_versions: This table stores general information about each network blog ID, database version, and date of last update.

check wp_registration_log: This table stores information about registered users, including

• Unique user numerical ID

• User e-mail address

• User IP address

• User Blog ID

• The date the user registered

check wp_signups: This table stores information about user sign-ups, including all the information from the wp_registration_log table, the date the user account was activated, and the unique activation key the user accessed during the sign-up process.

check wp_site: This table stores information about your main installation site, including the site ID, domain, and server path.

check wp_sitemeta: This table stores all the information about the multisite configurations set after you install the multisite feature. See Book VIII.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.199.184