Why do I need to use a database in a Web page?
World Wide Web (WWW) is more than just a place to provide information. If you have something, make a website, you can share it with people all over the world. But this is not a very easy thing to do. As the Web site becomes larger, you may experience problems like this:
The site contains so many things that visitors are not able to get what they want quickly. The problem is fatal to a website in a way.
The visitor wants to provide you with information, and the information must be saved for later use.
The above two problems, can be solved through the database!
In the world of WWW, databases are everywhere. As big as Yahoo! , Amazon, EBay, small to a simple message board, you can see the database in the arena. It can even be said that the database is the basis of all advanced applications.
Why use PHP and MySQL as I know, almost all of the major commercial web databases are sql-based. The most popular of these may be Oracle. It's powerful, of course, and it's expensive. SQL is not an application, but a language, shorthand for Structured query Language (Structured Query language) to manipulate and query the database.
In recent years, some companies have developed "open code" SQL applications, the best-known of which may be MySQL. It's not just free, but it does not perform as well as Oracle for the average medium and small database applications.
To run MySQL on a Web site, you need a scripting language to interact with the database. In the past, Perl was the most popular. But now it looks like PHP is a little better. Don't ask me what's the difference between them? I used to use Perl and it worked very well, but now it seems like everyone likes to use PHP. Its popularity certainly has its reason.
Let's take a look at how PHP works. Take a look at the following code:
< html>
< body>
< ?php
print "Hello, world.";
?>
< /body>
< /html>
When this page is requested, it will display "Hello, World" in the browser.
As you can see, the PHP script is embedded in the HTML file. It with "<"? "To begin with?" > "End. More than that, we can even embed HTML tags in PHP scripts:
< ?php
print "< html>";
print "< body>";
print "Hello, world.";
print "< /body>";
print "< /html>";
?>
Two methods are the same, the effect is the same. But in some special cases, it's more convenient to choose one of them.
PHP's Prints statement
The simplest interaction between PHP and HTML is achieved through the print statement:
< ?php
print "Hello, world.";
?>
Print is the simplest and most used function to display some text in a browser window, the Echo function is similar to print, but you can use the "," number to separate multiple items to display, which is more convenient when mixing string constants and variable display.
There is also a printf function to format the output of the number. You can use a number as an integer or a scientific notation to show it.
In these functions, the use of parentheses is different:
Echo must not have parentheses, printf, but it has to have print. To display a string or a number is simple, just follow the variable name or constant to the print statement. However, if you want to display an array, is it also written like this:
Print $myarray;
Its output will be "array", and PHP tells you $myarray is an array. This is useful when you are unsure whether a variable is an array, but now we want to see the contents of the array.