Building a database-driven Web site with PHP and MySQL (iv)

Source: Internet
Author: User
Tags functions mysql mysql client numeric value one table variables php file variable
mysql| Data | Database summary

In this chapter, we will introduce the scripting language for this server side of PHP. We will see that this language can well support communication with the MySQL database.

(2002-08-29-14:10:52)

--------------------------------------------------------------------------------
By Wing, Origin: Linuxaid


Chapter Three: Introduction to PHP

In the previous chapter, we learned how to use the MySQL database engine to store a list of jokes in a simple database containing only one table called jokes. At that time, we did this by entering the SQL command (query statement) on the MySQL client's command line. In this chapter, we will introduce the scripting language for this server side of PHP. We will see that this language can well support communication with the MySQL database.

Introduction to PHP

As we mentioned earlier, PHP is a server-side scripting language. This concept may be difficult to understand, especially if you have used HTML and JavaScript to design a Web page before. A server-side script is a bit like JavaScript in many places, both of which allow you to insert programs (scripts) into the HTML of a Web page. As a result, these scripts allow you to control what is displayed in the browser window, which is obviously much more flexible than using HTML directly.

The key difference between JavaScript and PHP is that when a Web browser interprets JavaScript, the Web page that contains the script is already downloaded, and for server-side scripting like PHP, the job is explained by the server completely before the page is sent to the browser. Once the explanation is complete, the PHP code in the Web page will be replaced by the result of the script running, and the viewer is looking at the standard HTML file. Scripts are handled entirely by the server. This is the origin of its name: server-side scripting programs.

Let's take a look back at the example today.php we established in the first chapter:


<HTML>
<HEAD>
<title>today ' s date</title>
</HEAD>
<BODY>
<p>today ' s Date (according to this WEB server) is</p>
<?ph
Echo (Date ("L, F DS Y."));
?>
</BODY>
</HTML>



The vast majority of this program is in HTML format. Only the lines in the middle are written in PHP. Represents "End PHP code". The Web server processes all content before this two identifier and converts it to standard HTML code before sending the Web page to the browser requesting it. The page that the browser receives will be like this:


<HTML>
<HEAD>
<title>today ' s date</title>
</HEAD>
<BODY>
<p>today ' s Date (according to this WEB server) is</p>
Wednesday, June 7th 2000.
</BODY>
</HTML>



Note that all of the PHP code is not displayed now. They are replaced by the corresponding standard HTML, and with this example we can see several advantages of server-side scripting:

There are no browser-compatible issues. The PHP script is only explained by the Web server, so we don't have to worry about whether the language you're using can be supported by your visitor's browser.

Access to server-side resources. In the example above, we put the date of the Web server in the Web page. And when we're using JavaScript, to do the same thing, we can only display the date of the computer that the Web browser is running on. This example is also not typical for using server-side resources, and in fact we can easily insert additional information that can only be invoked by scripts that run on the server, such as information stored in a MySQL database on a Web server.

Simplifies the loading of the client. JavaScript can significantly reduce the speed at which Web pages are displayed, because browsers first have to run JavaScript scripts before the Web page is displayed. For server-side scripting, the burden will be borne by the Web server alone.

Basic syntax and commands

For anyone who has used C, C + +, Java, JavaScript, Perl, or other languages originating in C, PHP's syntax will make you feel very familiar. A PHP script contains a series of commands (statements) that the Web server must process in turn. As with the other languages mentioned above, the PHP statement always ends with a semicolon (;).

This is a typical PHP statement:

Echo ("This is a test!");


This statement calls a built-in function called Echo, and passes it to a string that says: "This is a test! ”。 Built-in functions you can see that "you don't need another description of PHP to know what to do" thing. PHP has a lot of built-in functions that we can use to do a lot of things, ranging from sending e-mail to processing different types of data stored in a database is their jurisdiction. The Echo function simply inserts the text that is passed to it into the current position of the HTML page. You can take a look at the following example:


<HTML>
<HEAD>
<TITLE> Simple PHP Example </TITLE>
</HEAD>
<BODY>
<p><?php Echo ("This is a <b>test</b>!"); ?></p>
</BODY>
</HTML>



If you name the code test.php (or TEST.PHP3, if your web host hasn't configured the. php file as a PHP script) and put it on your Web server, the browser will see a page like this:


<HTML>
<HEAD>
<TITLE> Simple PHP Example </TITLE>
</HEAD>
<BODY>
<p>this is a <B>test</B>!</P>
</BODY>
</HTML>



Note that all text including HTML identifiers (and) is correctly displayed.

You may be wondering why we use parentheses and quotes here. Quotes in PHP are used to flag the start and end of a string. The parentheses have a double function, first, it indicates that echo is a function you want to call, and secondly, it flags the beginning and end of the function's arguments. Through the parameters, you can direct your function to do. For the echo function, we just need to give it a string to display on the page, but we'll see that some functions have several arguments (in which case we separate the arguments with commas). Even for functions without parameters, we still need parentheses, except that we don't have to enter anything in the middle of parentheses.

Variables and operators

Variables in PHP are the same as variables in many other programs. When it is not initialized, the variable is simply the name of an imaginary container, which can hold any numeric value. The following statement establishes a name $testvariable (all variables in PHP start with a dollar character) and assigns it to 3:

$testvariable = 3;


PHP is a "relaxed type" language, which means that a variable can contain any type of data (numbers, strings, or other types), and that its type is freely convertible during its lifetime. So if you have such a statement after the above statement, this means that we have $testvariable a value to our existing variable. This variable changes from a numeric variable to a string variable:

$testvariable = "Three";


The equal number we use in the two above statements is called the assignment operator, which is used to assign a value to a variable. In addition, we have some operators that perform mathematical operations:


$testvariable = 1 + 1; The variable is assigned a value of 2.
$testvariable = 1-1; The variable is assigned a value of 0.
$testvariable = 2 * 2; The variable is assigned a value of 4.
$testvariable = 2/2; The variable is assigned a value of 1.



There is a comment at the end of each line above. Annotations Explain the purpose of our code by inserting explanatory text, and it notifies the PHP interpreter that it ignores the interpretation of this part of the statement. Comments begin with//start and end within the same line. If you are familiar with the/*/style annotations in other languages, you can also use this in PHP. In our tutorial, we often use annotations to explain the program.

Now back to the four statements above, four operators are used to perform addition, subtraction, multiplication, and division operations. In addition, there is a run-time character that is used to concatenate strings:


Assign the variable to "Hi there!".
$testvariable = "Hi". " There! ";



Variables can be applied to many places where actual values are used. You can look at the following example:


$var 1 = "PHP"; Assign $var1 to "PHP"
$var 2 = 5; Assign a $VAR2 value of 5
$var 3 = $var 2 + 1; Assign a $VAR3 value of 6
$var 2 = $var 1; Assign $var2 to "PHP"
Echo ($var 1); Output "PHP"
Echo ($var 2); Output "PHP"
Echo ($var 3); Output 6
Echo ($var 1. "rules!"); Output "PHP rules!"
Echo ("$var 1 rules!"); Output "PHP rules!"
Echo (' $var 1 rules! '); Output ' $var 1 rules! '



Please pay special attention to the last two lines, you can include a variable name directly in a string, if you enclose it in double quotes, you will use the variable's value instead of the variable name. And the last line proves that if you're using single quotes, you're not going to make this conversion.

User interaction and Forms

For many of the PHP applications we are interested in, the most basic function is to implement the interaction with the user browsing the page. If you are familiar with JavaScript, you may be accustomed to a transactional pattern that responds directly to many of the user's actions (such as moving the mouse to a connection on the page). For a server-side script such as PHP, it uses a much smaller range of activities to interact with the user, and the interaction with the user occurs only when the user makes a request to the server and the server responds with a dynamic page.

The key to user interaction with PHP is to understand the message that may be contained in a request made by a user to a new Web page. As we'll see, PHP makes this work extremely simple.

The easiest way to do this is to use a URL query string to send information in a page request. If you have ever seen a URL that contains a question mark after a filename, that is the technique used. Let's take a look at a simple example. Create a standard HTML file (not necessarily using the. php extension, which will not contain any PHP code), and add the following connection to the file:

Hi, I ' m kevin!


This is a connection to a file called welcome.php, but while connecting to the file, we also passed a variable in the page request. This variable is passed as part of the query string, behind the question mark in the URL. The variable is named name, and its value is Kevin. That is, we built a connection that loads welcome.php and tells the PHP code contained in the file: Name equals Kevin.

To be clear about what's good for us, we need to see welcome.php. Create the same as a new HTML file, but this time, remember to use the. php extension, which tells the Web server that there are some PHP code in this file that needs to be explained. If your Web server does not accept. php as the extension of the PHP file, you may need to change it to WELCOME.PHP3 (in which case you will need to adjust the connection in the above code accordingly). In this new file, enter the following:


<?php
Echo ("Welcome to our Web site, $name!");
?>



Now, if you use the first file to load the second file, you will see that this page shows "Welcome to our Web site, kevin!", the value of the variable passed through the URL's query string is automatically given a variable called $name, We show this passing variable in a paragraph of text.

You can also pass several variables through the query string if you need to. Let's take a look at a slightly more complex version of this example. Change the connection in the HTML file to:



Hi, I ' m Kevin yank!



This time, we passed two variables: FirstName and LastName. These variables are separated by the & symbol in the query string. You can pass more variables, as long as you separate each name=value from the & symbol.

As mentioned earlier, we can use the values of these two variables in our welcome.php file:


<?php
Echo ("Welcome to our Web site,"
$firstname $lastname! ");
?>




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.